htmlunit 爬取百度云资源

htmlunit 爬取百度云资源

这里我们给出一个htmlunit实例,爬百度云;

为了更好的体现htmlunit的优势,我们先用httpclient爬下;

比如 https://pan.baidu.com/share/home?uk=305605848#category/type=0 

这个是我的百度云用户首页

htmlunit 爬取百度云资源_第1张图片

我们现在要爬用户分享的文件 ;

我们先用httpclient:

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package  com.open1111;
 
import  java.io.IOException;
 
import  org.apache.http.HttpEntity;
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.HttpClients;
import  org.apache.http.util.EntityUtils;
 
public  class  BlogCrawler {
 
     public  static  void  main(String[] args)  throws  ClientProtocolException, IOException {
         CloseableHttpClient httpclient = HttpClients.createDefault();  // 创建httpclient实例
         HttpGet httpget =  new  HttpGet( "https://pan.baidu.com/share/home?uk=305605848#category/type=0" ); // 创建httpget实例
         httpget.setHeader( "User-Agent" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0" );  // 设置请求头消息User-Agent
         CloseableHttpResponse response = httpclient.execute(httpget);  // 执行get请求
         HttpEntity entity=response.getEntity();  // 获取返回实体
         System.out.println( "网页内容:" +EntityUtils.toString(entity,  "utf-8" ));  // 指定编码打印网页内容
         response.close();  // 关闭流和释放系统资源
     }
}

运行结果:

htmlunit 爬取百度云资源_第2张图片

我们得到的是这东西,没获取到数据,原因就是百度云的数据加载 是通过ajax加载以及js渲染上去的,所以用httpclient搞不定;


这时候,我们用htmlunit搞下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package  com.open1111;
 
import  java.io.IOException;
import  java.net.MalformedURLException;
 
import  com.gargoylesoftware.htmlunit.BrowserVersion;
import  com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import  com.gargoylesoftware.htmlunit.WebClient;
import  com.gargoylesoftware.htmlunit.html.HtmlPage;
 
public  class  HtmlUnitTest7 {
 
     public  static  void  main(String[] args){
         WebClient webClient= new  WebClient(BrowserVersion.FIREFOX_52);
         HtmlPage page;
         try  {
             page = webClient.getPage( "https://pan.baidu.com/share/home?uk=305605848#category/type=0" );
             Thread.sleep( 10000 );  // 休息10秒钟 等待htmlunit执行js
             System.out.println(page.asXml());
         catch  (FailingHttpStatusCodeException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         catch  (MalformedURLException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         catch  (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         catch  (InterruptedException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         } finally {
             webClient.close();  // 关闭客户端 释放内存
         }
     }
}


运行输出:

htmlunit 爬取百度云资源_第3张图片


Htmlunit搞数据搞出来了。。

代码用了sleep 10秒  主要是因为htmlunit执行ajax js也是需要一定的时候 具体多长时间 不定,并且htmlunit没有提供类似假如执行完ajax js后的事件或者提醒api,所以我们只能人工去判断 比如每个5秒去判断一次,尝试三次机会;



你可能感兴趣的:(爬虫)