V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
yakczh
V2EX  ›  Python

pyquery 如何传 UserAgent

  •  
  •   yakczh · 2014-02-24 09:02:21 +08:00 · 4161 次点击
    这是一个创建于 3926 天前的主题,其中的信息可能已经有所发展或是发生改变。
    照上面的文档 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
    6 条回复    1970-01-01 08:00:00 +08:00
    yangg
        1
    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
        2
    yakczh  
    OP
       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
        3
    pandada8  
       2014-02-24 10:35:58 +08:00
    为什么不用Requests?
    yangg
        4
    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
        5
    yakczh  
    OP
       2014-02-24 11:43:21 +08:00
    @yangg 那说明你安装了requests 我安装了requeust以后,再请求就显示正确的ua, pip uninstall requests 以后,再请求又显示 User-Agent: Python-urllib/3.2 ,好象pyq是自动选择的,真是奇怪
    yakczh
        6
    yakczh  
    OP
       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没传进去
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2837 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 08:43 · PVG 16:43 · LAX 00:43 · JFK 03:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.