package com.search.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.search.entitys.Message;
import com.search.interfaces.ISpider;

/**
 * @Description: TODO(爬虫实现类)
 * @Date: 2017年1月15日
 * @author: luoshao
 * @Copyright (c) 2017, www.panyixia.cn , 792435323@qq.com All Rights Reserved.
 */
public class Spider implements ISpider {

	protected String crawlSource(String url) throws ClientProtocolException,
			IOException {
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();
		CloseableHttpResponse httpResponse = httpClient.execute(new HttpGet(url));
		String result = EntityUtils.toString(httpResponse.getEntity());
		return result;
	}

	@Override
	public List<Message> getList(String url) {
		String result = null;
		List<Message> list = new ArrayList<Message>();
		Message msg = null;
		try {
			result = crawlSource(url);
			Document doc = Jsoup.parse(result);
			Elements es = doc.select(".b_algo h2 a");

			for (Element e : es) {
				msg = new Message();
				msg.setTitle(e.html());
				msg.setUrl(e.attr("href"));
				msg.setContent(e.parent().parent().select(".b_caption").html());
				list.add(msg);
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

}
最近下载更多
HuangLin544  LV3 2022年4月2日
西凉河的葛三叔  LV3 2022年1月28日
wjh12345654321  LV14 2021年7月17日
无题  LV1 2021年5月11日
全国12345  LV3 2020年5月18日
luocheng  LV18 2020年1月13日
oushao  LV10 2019年12月3日
fuyu103  LV3 2019年11月8日
jiang1997  LV6 2019年11月5日
fenghuang8  LV22 2019年5月22日
最近浏览更多
molu123456 2023年10月10日
暂无贡献等级
快乐的风铃语 2023年8月29日
暂无贡献等级
z493331203 2023年6月2日
暂无贡献等级
cgfeng12345  LV10 2022年10月24日
最小白K 2022年10月6日
暂无贡献等级
HuangLin544  LV3 2022年4月2日
HappierLee  LV1 2022年3月15日
西凉河的葛三叔  LV3 2022年1月28日
3089559272  LV11 2021年12月17日
^_^ZHW 2021年10月5日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友