推荐学习书目
› 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 · Feb 24, 2014 · 4791 views
    This topic created in 4633 days ago, the information mentioned may be changed or developed.
    照上面的文档 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 replies  •  1970-01-01 08:00:00 +08:00
    yangg
        1
    yangg  
       Feb 24, 2014
    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
       Feb 24, 2014
    @yangg
    GET / HTTP/1.1
    Accept-Encoding: identity
    Host: localhost:8080
    Connection: close
    User-Agent: Python-urllib/3.2
    pandada8
        3
    pandada8  
       Feb 24, 2014
    为什么不用Requests?
    yangg
        4
    yangg  
       Feb 24, 2014
    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
       Feb 24, 2014
    @yangg 那说明你安装了requests 我安装了requeust以后,再请求就显示正确的ua, pip uninstall requests 以后,再请求又显示 User-Agent: Python-urllib/3.2 ,好象pyq是自动选择的,真是奇怪
    yakczh
        6
    yakczh  
    OP
       Feb 24, 2014
    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没传进去
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4542 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 00:17 · PVG 08:17 · LAX 16:17 · JFK 19:17
    ♥ Do have faith in what you're doing.