Jsoup学习 JAVA爬虫爬取美女网站 JAVA爬虫爬取美图网站 爬虫

最近对爬虫起了兴趣,但是网上都说做爬虫最好得语言是py。但是我只会java,所以就想能不能用java实现一个爬虫,百度搜索发现,其实java也有很多优秀得开源爬虫框架,包括Gecco,webmagic,Jsoup等等非常多得优秀开源框架,可以让我们在不是十分熟悉正则表达式得情况下也能实现爬虫爬取数据。

本案例使用Jsoup解析网页。使用Jsoup可以很方便的使用类似Jquery得选择器语法来选择html中得元素以提取数据。十分方便

这里有Jsoup得使用简介:http://www.open-open.com/jsoup/


本爬虫得目标网站是https://www.4493.com得美图板块。

先上成果,质量还是很高得,由于硬盘容量有限,我只爬了十页数据。就已经一个多GB了。



接下来是代码:
package .crawl.mote;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;


import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


/**
 * 爬取4493美图图片使用jsoup和使用正则表达式各一则
 * @author jiuwei
 *
 */
public class main4493 {
	public static final String URL = "https://www.4493.com";
	/**
	 * 性感美女
	 */
	public static String XGMN = "https://www.4493.com/xingganmote/";
	public static int xgmnPageCount = 10;
	public static final String XGMN_DIR = "性感美女";
	
	/**
	 * 丝袜美腿
	 */
	public static String SWMT = "https://www.4493.com/siwameitui/";
	public static int swmtPageCount = 0;
	public static final String SWMT_DIR = "丝袜美腿";
	/**
	 * 唯美写真
	 */
	public static String WMXZ = "https://www.4493.com/weimeixiezhen/";
	public static int wmxzPageCount = 0;
	public static final String WMXZ_DIR = "唯美写真";
	/**
	 * 网络美女
	 */
	public static String WLMN = "https://www.4493.com/wangluomeinv/";
	public static int wlmnPageCount = 0;
	public static final String WLMN_DIR = "网络美女";
	/**
	 * 高清美女
	 */
	public static String GQMN = "https://www.4493.com/gaoqingmeinv/";
	public static int gqmnPageCount = 0;
	public static final String GQMN_DIR = "高清美女";
	/**
	 * 模特美女
	 */
	public static String MTMN = "https://www.4493.com/motemeinv/";
	public static int mtmnPageCount = 0;
	public static final String MTMN_DIR = "模特美女";
	/**
	 * 体育美女
	 */
	public static String TYMN = "https://www.4493.com/tiyumeinv/";
	public static int tymnPageCount = 0;
	public static final String TYMN_DIR = "体育美女";
	/**
	 * 动漫美女
	 */
	public static String DMMN = "https://www.4493.com/dongmanmeinv/";
	public static int dmmnPageCount = 0;
	public static final String DMMN_DIR = "动漫美女";
	public static File DIR = new File( "d:\\4493\\" );


	public static void main(String[] args) throws Exception {
		for(int i = 0; i<xgmnPageCount;i++){
			String url = XGMN;
			if(i>0){
				url = XGMN + "index-"+(i+1)+".htm";
			}
			List<Model> list = getPage(url);//获取所有得图片页对象
			downloadJpg(list,XGMN_DIR);
		}
	}
	
	/**
	 * 取得当前页对象
	 * @param url
	 * @return
	 */
	public static List<Model> getPage(String url){
		/**
		 * 使用jsoup请求页面并分析
		 */
		List<Model> pageUrl = null;
		Document document = getDocument(url);//获取主页面以取得本页面所有得页得url
		pageUrl = new ArrayList<Model>();//存放所有得页url
		Elements ulElement = document.select("ul.clearfix");//类选择器选择ul
		Elements liElement = ulElement.select("li");//类选择器选择li
		//取得当前页得每一页得对象
		for(int i = 0; i < liElement.size(); i++){
			List<String> imgUrlList = new ArrayList<String>();
			Element e = liElement.get(i);
			Model model = new Model();
			model.setTitle(e.select("span").text());
			String aurl = e.select("a").attr("href");
			aurl = aurl.substring(0 , aurl.length()-6);
			model.setUrl(URL+aurl+".htm");
			model.setGxsj(e.select("b.b1").text());
			Document document1 = getDocument(model.getUrl());
			Elements divElement = document1.select("div.picsbox");//取得图片div
			Elements imgElement = divElement.select("img");//从div中取得所有得img标签
			for(int j = 0;j<imgElement.size();j++){
				imgUrlList.add(imgElement.get(j).attr("src"));
			}
			model.setImgUrl(imgUrlList);
			model.setZsl(imgUrlList.size());//总数量为但也浏览页面得所有url得size
			pageUrl.add(model);
		}
		return pageUrl;
	}
	
	public static void downloadJpg(List<Model> list,String dir2){
		//分别下载图片到硬盘,按照标题分开
		File file = new File(DIR,dir2);
		if(!file.exists()){
			file.mkdirs();//创建多级文件夹
		}
		System.out.println( file + ":创建成功" );
		for(int i =0; i<list.size(); i++){
			File file1 = new File(file,list.get(i).getTitle());
			if(!file1.exists()){
				file1.mkdirs();//创建多级文件夹
			}
			System.out.println( file1 + ":创建成功" );
			
			List<String> srcList = list.get(i).getImgUrl();
			for(int j = 0; j<srcList.size(); j++){
				String src = srcList.get(j);
				File file2 = new File( file1, (j+1) + ".jpg" );
				if(file2.exists()){
					System.out.println(file2 + "已经存在,跳过;");
					continue;
				}
				URL url;
				try {
					url = new URL(src);
					BufferedInputStream biStream = new BufferedInputStream(url.openStream());
					BufferedOutputStream ouStream = new BufferedOutputStream(new FileOutputStream(file2)); 
					System.out.println( list.get(i).getTitle() + ":" + src + "开始下载..." );
					byte[] buf = new byte[1024];
					int len;
					while((len = biStream.read(buf)) != -1){
						ouStream.write(buf,0,len);
					}
					biStream.close();
					ouStream.close();
					System.out.println( list.get(i).getTitle() + "下载完成!");
				} catch (MalformedURLException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
			}
		}
	}
	
	
	
	public static Document getDocument(String url){
		Connection connect = Jsoup.connect(url);
		Document document = null;
		try {
			document = connect.timeout(100000).get();
			return document;
		} catch (IOException e) {
			System.out.println("连接超时!!");
			e.printStackTrace();
		}
		return document;
	}
	
	
	
}
详细得代码已经打包,连接在这里,这里只为练习Jsoup,所以功能不是十分得完善。
http://download.youkuaiyun.com/download/wangqq335/10106691,欢迎前辈指正!!


介绍 美图网站千千万,美图自己说了算!本源码由@香谢枫林开发,首页图片做了浏览器窗口自适应,最大化占满PC浏览器和移动浏览器的窗口,并且防止出现滚动条。 源码截图 功能介绍 首页图片设置了4个点击功能区,分别是:上一张、下一张、全部随机、套图集随机(为了丰富移动端操作) 搜索功能注释隐藏了,想用的同学自己打开发开者模式解除注释即可 随机按钮:仅在右上角下拉菜单选中的图集中随机 随机All按钮:全部图片中随机 自动随机:仅在右上角下拉菜单选中的图集中自动随机(间隔1.5s) 自动随机All:全部图片中自动随机(间隔1.5s) 自动浏览:自动下一张(间隔1.5s) 套图1:点击跳转新窗口,查看套图所有图片(观赏模式一) 套图2:点击跳转新窗口,查看套图所有图片(观赏模式二) 索引:点击跳转新窗口,查看套图集 收藏:收藏图片,点击”Count“也能收藏 收藏列表:点击跳转新窗口,观赏收藏图片 #注:键盘任意键或者点击图片即可解除自动浏览状态; 按键介绍 @上一张:<–(左方向键) @下一张:–>(右方向键) @全部图集随机:(上方向键 或 alt键) @所选图集随机:(下方向键或shift键) @跳转套图第一张:(空格键) @收藏:(?问号键) @浏览整套图片(模式1):({左大括号) @浏览整套图片(模式2):(}右大括号) @关闭标签页(针对跳转的页面):(\顿号) @自动浏览(随机全部图集1.5s间隔):(”冒号键) @自动浏览(随机所选图集1.5s间隔):(;分号键) @回看随机历史:(《左书名号键) @自动浏览(下一张1.5s间隔):(》右书名号键) #注:任意键结束自动浏览状态;”{}”这两个按键在套图浏览页面可以切换浏览模式 使用说明 第一步:本地建库,把数据库下载到本地(线上数据库连接参数在global.php文件里)也可以自己创建数据库,需要3个基础表,分别是pc_dic_lanvshen、pc_dic_toxic、po_toxic(在数据库文件夹下) 第二步:修改数据库配置,global.php文件的database配置,设置成连接自己本地的数据库;到此,启动网站,你可以愉快的赏图了! 1.想改按键怎么办?在js/index.js文件中,有具体的按键对应功能说明,想改按键,只需要修改对应的键值即可(keyCode键值问度娘吧) 2.搜过功能有木有?在index.php文件中,取消注释id为tb和tj的两行代码即可显示搜索功能 说明 我做了3个美图网站的数据爬取功能,分别对应3个文件:lanvshen.py,lanvshen_qtw.py,lanvshen_mtll.py(顶部有注释对应哪个网站) 第一步:修改数据库配置文件conf/mysql_db.py,修改数据库连接,改成自己本地的数据库; 第二步:找到自己想要爬取的页面,修改主函数信息即可,只需要修改4个参数,如下图所示 注:Python记得下载好对应的插件(requests,re,time,random,BeautifulSoup)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值