`
gcgmh
  • 浏览: 348671 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

httpclient 设置user-agent

阅读更多
前些天在csdn上看到有人说dianping.com上的东西没法抓取,我就用htmlparser试了试,果然不行。看返回结果
Server returned HTTP response code: 500 for URL: http://www.dianping.com/shop/2212912
不能使用:然后想到换httpclient来试试:

HttpClient hc=new HttpClient();
GetMethod gm=new GetMethod("http://www.dianping.com/shop/1968937");
hc.executeMethod(gm);
System.out.print(gm.getResponseBodyAsString());

返回的数据:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>提示_大众点评网</title><style type="text/css">html{background:#f7f7f7;}body{background:#fff;color:#333;font-family:"MicrosoftYaHei","微软雅黑",Verdana,Arial;margin:2em auto 0 auto;width:700px;padding:1em 2em;-moz-border-radius:11px;-khtml-border-radius:11px;-webkit-border-radius:11px;border-radius:11px;border:1px solid #dfdfdf;}a{color:#2583ad;text-decoration:none;}a:hover{color:#d54e21;}h1{border-bottom:1px solid #dadada;clear:both;color:#666;margin:5px 0 5px 0;padding:0;padding-bottom:1px;}p{text-align:center;}sub{display:block;margin:0;padding:0;color:#aaa;font-size:11px;text-align:right;}</style></head><body><h1 id="logo" style="text-align: center"><img alt="dianping.com" src="http://i1.dpfile.com/s/img/simplelogo.gif" /></h1><p>对不起,您的访问存在某些问题。<br />如果您是正常访问,请与<a href="mailto:spam@dianping.com">spam@dianping.com</a>联系,并附上以下信息:<br /><textarea rows="10" cols="80">401
221.221.153.137
jakarta commons-httpclient/3.0</textarea></p><sub>401</sub></body></html>

大家可以把他复制到html看看效果,主要是提示我的浏览器错误:使用httpclient默认为jakarta commons-httpclient/3.0
我就换了user-agent试试
在上面的代码中加入

hc.getParams().setParameter(HttpMethodParams.USER_AGENT,"Mozilla/5.0 (X11; U; Linux i686; zh-CN; rv:1.9.1.2) Gecko/20090803 Fedora/3.5.2-2.fc11 Firefox/3.5.2");//设置信息

然后再提交就行了。。这个网站是通过判断user-agent来实现是不是正常的访问.
分享到:
评论
1 楼 mypotatolove 2012-07-23  
我现在想做用HttpClient从微博中爬取微博动态,能不能跟我讲一下其中的原理和具体实现要做什么啊?

相关推荐

    useragent:为每个请求添加一个 `User-Agent` 标头

    去取用户代理 为每个请求添加一个User-Agent标头。安装 npm install --save go-fetch-useragent用法 var HttpClient = require('go-fetch');var useragent = require('go-fetch-useragent');HttpClient() .use...

    HttpClient:php使用socket模拟post、get操作,支持http、socket4、5代理

    $ http -&gt; set_header ( 'User-Agent' , 'Mozilla/5.0 (Windows NT 5.1; rv:13.0) Gecko/20100101 Firefox/13.0.1' ); $ http -&gt; get ( 'http://www.example.com/' ); echo $ http -&gt; get_body (); POST请求 $ ...

    15.tornado的httpclient模块使用介绍.zip

    User-Agent设置和IP代理的应用 验证码自动识别方法 文件IO操作 文件读写基本操作 CSV和Excel文件的处理 文本文件编码和解码 数据存储与持久化 数据库的使用和操作(如MySQL、MongoDB等) 数据存储格式的选择和优化...

    Java使用HttpClient和HtmlParser实现的爬虫Demo.zip

    遵守规则: 为避免对网站造成过大负担或触发反爬虫机制,爬虫需要遵守网站的robots.txt协议,限制访问频率和深度,并模拟人类访问行为,如设置User-Agent。 反爬虫应对: 由于爬虫的存在,一些网站采取了反爬虫措施...

    parse-body:解析并连接响应正文

    去获取解析正文 连接并解析响应流。 安装 npm install --save go-fetch-parse-body... .get('https://api.github.com/repos/go-fetch-js/parse-body', {'User-Agent': 'go-fetch'}, function(error, response) { con

    http-client.jsx:适用于Photoshop的微型http客户端

    http-client.jsx 适用于Photoshop的微型...client.full.jsx"var client = new HttpClient ( 'localhost' , { encoding : 'binary' , port : 80 , timeout : 10 , headers : { "User-Agent" : "Adobe ExtendScript" } }

    Java网络爬虫小说下载器.zip

    遵守规则: 为避免对网站造成过大负担或触发反爬虫机制,爬虫需要遵守网站的robots.txt协议,限制访问频率和深度,并模拟人类访问行为,如设置User-Agent。 反爬虫应对: 由于爬虫的存在,一些网站采取了反爬虫措施...

    Gecco是一款用java语言开发的轻量化的易用的网络爬虫.zip

    遵守规则: 为避免对网站造成过大负担或触发反爬虫机制,爬虫需要遵守网站的robots.txt协议,限制访问频率和深度,并模拟人类访问行为,如设置User-Agent。 反爬虫应对: 由于爬虫的存在,一些网站采取了反爬虫措施...

    蜂巢爬虫系统 .zip

    遵守规则: 为避免对网站造成过大负担或触发反爬虫机制,爬虫需要遵守网站的robots.txt协议,限制访问频率和深度,并模拟人类访问行为,如设置User-Agent。 反爬虫应对: 由于爬虫的存在,一些网站采取了反爬虫措施...

    电商网站商品信息爬虫.zip

    遵守规则: 为避免对网站造成过大负担或触发反爬虫机制,爬虫需要遵守网站的robots.txt协议,限制访问频率和深度,并模拟人类访问行为,如设置User-Agent。 反爬虫应对: 由于爬虫的存在,一些网站采取了反爬虫措施...

    详解【python】str与json类型转换

    str = '{"accessToken": "521de21161b23988173e6f7f48f9ee96e28", "User-Agent": "Apache-HttpClient/4.5.2 (Java/1.8.0_131)"}' j = json.loads(str) print(j) print(type(j)) 输出 {‘accessToken’: ‘521de211

    nginx 防盗链防爬虫配置详解

    禁止Scrapy等工具的抓取 if ($http_user_agent ~* (Scrapy|Curl|HttpClient)) { return 403; } 禁止指定UA及UA为空的访问 #forbidden Scrapy if ($http_user_agent ~* (Scrapy|Curl|HttpClient)) { return 403; ...

    Nginx反爬虫策略,防止UA抓取网站

    if ($http_user_agent ~* (Scrapy|Curl|HttpClient)) { return 403; } #禁止指定UA及UA为空的访问 if ($http_user_agent ~ WinHttp|WebZIP|FetchURL|node-superagent|java/|FeedDemon|Jullo|JikeSpider|Indy ...

    jdk-9.0.1_doc-all 最新版

    jdk.incubator.httpclient Defines the high-level HTTP and WebSocket API. jdk.jartool Defines tools for manipulating Java Archive (JAR) files, including the jar and jarsigner tools. jdk.javadoc ...

Global site tag (gtag.js) - Google Analytics