推荐学习书目
› 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
katyang
V2EX  ›  Python

selenium 不能加 headers,那该怎么办?

  •  
  •   katyang · Aug 21, 2016 · 11274 views
    This topic created in 3730 days ago, the information mentioned may be changed or developed.

    想写个自动顶帖的脚本, 打算用 selenium webdriver 实例 的 execute_script 功能 回帖需要先登录,我就把 cookie 写在 headers 里面 这时才发现 selenium 好像不能加 headers ,那该怎么办?

    之前试了一下 requests.get(),得到的是<Response [200]=""> 所以就没有想过 requests.post()了

    13 replies  •  2016-08-27 10:12:19 +08:00
    ltux
    ltux
    Aug 21, 2016
    1
    不要直接用 requests.get() 和 requests.post(),这俩函数需要你手工管理 cookie 。请使用 requests.Session 类,可自动管 cookie ,用法也很简单,具体可看文档 http://docs.python-requests.org/en/master/user/advanced/#session-objects
    ltux
    ltux
    Aug 21, 2016
    2
    如果是用 selenium webdriver 的话, 也不需要手工管理 cookie 吧,这事儿浏览器就帮你做了
    ltux
    ltux
    Aug 21, 2016
    3
    "那该怎么办?" 使用 selenium webdriver 在登陆页输入登陆信息然后调用 submit() 或者 click() 提交表单就行了吧
    jason52
    jason52
    Aug 21, 2016 via Android
    4
    直接录制一下脚本,然后转成 Python 最简单。
    hebwjb
    hebwjb
    Aug 21, 2016❤️1
    5
    selenium 不需要关注什么 headers 、 cookie 之类的,完全模拟人工操作,定位元素、输入账号密码登录就可以了
    katyang
    katyang
    OP
    Aug 22, 2016
    6
    @hebwjb 谢谢指点
    katyang
    katyang
    OP
    Aug 22, 2016
    7
    @jason52 有什么好的录制和转换工具可以推荐一下吗^_^
    katyang
    katyang
    OP
    Aug 22, 2016
    8
    @ltux 谢谢! 可不可以说 selenium 比 requests 更强大呢?
    okampfer
    okampfer
    Aug 22, 2016
    9
    @katyang Selenium 会启动浏览器窗口并进行操作, requests 是发送和处理 http 请求。前者有界面更重量级,后者无界面更轻量级。前者因 selenium 本身缺陷可能不太稳定且费时,后者方便快捷。前者更接近真实用户操作(浏览器和用户的交互)常用于 UI 自动化测试,后者更 geek 更接近浏览器和服务器交互的本质。

    录制和转换的话用 Firefox 的插件 SeleniumIDE 即可。

    以我的经验,除非你想做 UI 测试,否则不需要用 selenium 。打开开发者工具研究 http 请求的规律用 requests 复现整个过程就行了。
    jetyang
    jetyang
    Aug 22, 2016
    10
    google 一下 selenium browsermob proxy 有惊喜
    practicer
    practicer
    Aug 22, 2016
    11
    正在写 phantomJS + header :
    dcap = dict(DesiredCapabilities.PHANTOMJS)
    dcap['phantomjs.page.settings.userAgent'] = 'Mozilla/5.0'
    driver = webdriver.PhantomJS('path/to/PhantomJS', desired_capabilities=dcap)
    driver.get(<your_url>)

    虽然加了 header 信息,但最后发现不加 header 目标网站也让我爬
    thekoc
    thekoc
    Aug 22, 2016
    12
    好像可以用浏览器的插件管理 headers
    twocucao
    twocucao
    Aug 27, 2016
    13
    我之前是通过 Python 脚本调用执行 Phantomjs 的 Js 脚本的.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1435 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 10:24 · PVG 18:24 · LAX 02:24 · JFK 05:24
    ♥ Do have faith in what you're doing.