pyquery 如何传 UserAgent

2014-02-24 09:02:21 +08:00
 yakczh
照上面的文档 http://pythonhosted.org/pyquery/scrap.html
url='http://localhost:8080/'
d = pyq(url, headers={'User-Agent': 'User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20140129 Firefox/24.0'})

但是服务器日志显示还是User-Agent: Python-urllib/3.2
4134 次点击
所在节点    Python
6 条回复
yangg
2014-02-24 10:00:16 +08:00
d = pyq(url, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20140129 Firefox/24.0'})
yakczh
2014-02-24 10:12:12 +08:00
@yangg
GET / HTTP/1.1
Accept-Encoding: identity
Host: localhost:8080
Connection: close
User-Agent: Python-urllib/3.2
pandada8
2014-02-24 10:35:58 +08:00
为什么不用Requests?
yangg
2014-02-24 10:55:52 +08:00
print pq('http://ifconfig.me/ua', headers={'user-agent': 'Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20140129 Firefox/24.0'})

没啥问题
yakczh
2014-02-24 11:43:21 +08:00
@yangg 那说明你安装了requests 我安装了requeust以后,再请求就显示正确的ua, pip uninstall requests 以后,再请求又显示 User-Agent: Python-urllib/3.2 ,好象pyq是自动选择的,真是奇怪
yakczh
2014-02-24 11:47:56 +08:00
http://pythonhosted.org/pyquery/scrap.html

By default it use python’s urllib.

If requests is installed then it will use it. This allow you to use most of requests parameters

但是按照缺省的urllib传headers没传进去

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/101521

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX