httpclient爬取网页

1、设置爬取的url

String url = "http://www.gametoutiao.com/toutiao/index.html";

2、建立爬取的客户端

HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(90000);
client.getHttpConnectionManager().getParams().setSoTimeout(90000);

3、建立爬取的请求头

HashMap<String, String> headerMap = new HashMap<>();
headerMap.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
headerMap.put("Accept-Language", "zh-CN,zh;q=0.8");
headerMap.put("Host", "http://www.gametoutiao.com");

4、创建请求的get方法

GetMethod getmethod = new GetMethod(url);
getmethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
getmethod.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);
getmethod.setRequestHeader("Accept-Encoding", "gzip,deflate");

5、将请求头添加到get方法中去

for(Entry<String, String> entry:headerMap.entrySet()){
    getmethod.addRequestHeader(entry.getKey(), entry.getValue());
}

6、执行方法

int statusCode = client.executeMethod(getmethod);
if(statusCode != HttpStatus.SC_OK){
    System.out.println("fail!!!!");
}

7、获得方法的返回结果

byte[] result_body = getmethod.getResponseBody();
String body = new String(result_body);

8、对结果的处理,找到最大的页数

Pattern pattern = Pattern.compile("(\\d+).html\">尾页");
Matcher matcher = pattern.matcher(body);
if(matcher.find()){
    System.out.println(matcher.group(1));
}else{
    System.out.println("not find");
}

9、获取每篇文章的title,url

//<li class="col-sm-4 col-md-4">
Pattern pattern_content = Pattern.compile("<li class=\"col-sm-4 col-md-4\">(.*?)</li>",Pattern.DOTALL);
Matcher matcher_content = pattern_content.matcher(body);
while(matcher_content.find()){
    String lis = matcher_content.group(1);
    // <img src="/d/file/toutiao/1603c85157af7077cc29bfa9256f68e8.jpg" width="220" height="145" alt="对游戏行业而言 垄断或是市场竞争的一种最优状态"/>
    Matcher m_title_url = Pattern.compile("<img src=\"(.*?)\" width=\"220\" height=\"145\" alt=\"(.*?)\"/>").matcher(lis);
    if(m_title_url.find()){
        System.out.println(m_title_url.group(1));
        System.out.println(m_title_url.group(2));
    }
}

10、释放链接

getmethod.releaseConnection();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值