博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
配置Nutch模拟浏览器以绕过反爬虫限制
阅读量:6954 次
发布时间:2019-06-27

本文共 5014 字,大约阅读时间需要 16 分钟。

hot3.png

当我们配置Nutch抓取 http://yangshangchuan.iteye.com 的时候,抓取的所有页面内容均为:您的访问请求被拒绝 ...... 这是最简单的反爬虫策略(该策略简单地读取HTTP请求头User-Agent的值来判断是人(浏览器)还是机器爬虫,我们只需要简单地配置Nutch来模拟浏览器(simulate web browser)就可以绕过这种限制。

 

nutch-default.xml中有5项配置是和User-Agent相关的:

 

  
http.agent.description
  
  
Further description of our bot- this text is used in  the User-Agent header.  It appears in parenthesis after the agent name.  
  
http.agent.url
  
  
A URL to advertise in the User-Agent header.  This will    appear in parenthesis after the agent name. Custom dictates that this   should be a URL of a page explaining the purpose and behavior of this   crawler.  
  
http.agent.email
  
  
An email address to advertise in the HTTP 'From' request   header and User-Agent header. A good practice is to mangle this   address (e.g. 'info at example dot com') to avoid spamming.  
  
http.agent.name
  
  
HTTP 'User-Agent' request header. MUST NOT be empty -   please set this to a single word uniquely related to your organization.  NOTE: You should also check other related properties: http.robots.agents http.agent.description http.agent.url http.agent.email http.agent.version  and set their values appropriately.  
  
http.agent.version
  
Nutch-1.7
  
A version string to advertise in the User-Agent    header.

 

在类nutch1.7/src/plugin/lib-http/src/java/org/apache/nutch/protocol/http/api/HttpBase.java中可以看到这5项配置是如何构成User-Agent的:

 

this.userAgent = getAgentString( conf.get("http.agent.name"),         conf.get("http.agent.version"),         conf.get("http.agent.description"),         conf.get("http.agent.url"),         conf.get("http.agent.email") );

 

  private static String getAgentString(String agentName,                                       String agentVersion,                                       String agentDesc,                                       String agentURL,                                       String agentEmail) {        if ( (agentName == null) || (agentName.trim().length() == 0) ) {      // TODO : NUTCH-258      if (LOGGER.isErrorEnabled()) {        LOGGER.error("No User-Agent string set (http.agent.name)!");      }    }        StringBuffer buf= new StringBuffer();        buf.append(agentName);    if (agentVersion != null) {      buf.append("/");      buf.append(agentVersion);    }    if ( ((agentDesc != null) && (agentDesc.length() != 0))    || ((agentEmail != null) && (agentEmail.length() != 0))    || ((agentURL != null) && (agentURL.length() != 0)) ) {      buf.append(" (");            if ((agentDesc != null) && (agentDesc.length() != 0)) {        buf.append(agentDesc);        if ( (agentURL != null) || (agentEmail != null) )          buf.append("; ");      }            if ((agentURL != null) && (agentURL.length() != 0)) {        buf.append(agentURL);        if (agentEmail != null)          buf.append("; ");      }            if ((agentEmail != null) && (agentEmail.length() != 0))        buf.append(agentEmail);            buf.append(")");    }    return buf.toString();  }

 

在类nutch1.7/src/plugin/protocol-http/src/java/org/apache/nutch/protocol/http/HttpResponse.java中使用User-Agent请求头,这里的http.getUserAgent()返回的userAgent就是HttpBase.java中的userAgent:

 

String userAgent = http.getUserAgent();if ((userAgent == null) || (userAgent.length() == 0)) {	if (Http.LOG.isErrorEnabled()) { Http.LOG.error("User-agent is not set!"); }} else {	reqStr.append("User-Agent: ");	reqStr.append(userAgent);	reqStr.append("\r\n");}

 

通过上面的分析可知:在nutch-site.xml中只需要增加如下几种配置之一便可以模拟一个特定的浏览器(Imitating a specific browser)

 

1、模拟Firefox浏览器:

 

http.agent.name
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko
http.agent.version
20100101 Firefox/27.0

 

2、模拟IE浏览器:

 

http.agent.name
Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident
http.agent.version
6.0)

 

3、模拟Chrome浏览器:

 

http.agent.name
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari
http.agent.version
537.36

 

4、模拟Safari浏览器:

 

http.agent.name
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari
http.agent.version
534.57.2

 

 

5、模拟Opera浏览器:

 

http.agent.name
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36 OPR
http.agent.version
19.0.1326.59

 

 

后记:查看User-Agent的方法:

1、

2、

3、

 

转载于:https://my.oschina.net/apdplat/blog/208457

你可能感兴趣的文章
idea 自动导包
查看>>
js高级技巧之密封对象
查看>>
为什么越来越少的人用 jQuery?
查看>>
java关于安卓,苹果输入表情数据库处理
查看>>
关于Java集合类迭代删除元素的一些坑
查看>>
vue附件名字显示打印机的解决方案
查看>>
mysql主从常见问题
查看>>
用网关zuul时,熔断hytrix里面的坑
查看>>
iOS 截屏
查看>>
ubuntu安装mysql后的配置
查看>>
Hive的三种Join方式
查看>>
Javascript 经典资料分享大家一起顶
查看>>
linux 系统 stty 的使用
查看>>
思科路由器×××的配置实例
查看>>
搭建Web服务器之Step2:VMware下虚拟机NAT联网设置
查看>>
Android 多线AsyncTask详解
查看>>
Puppet基础篇6-Puppet更新方式的选型
查看>>
我的友情链接
查看>>
使用AdminLTE
查看>>
Armstrong数
查看>>