python - Python 中的 Selenium PhantomJS 自定义 header

标签 python selenium phantomjs custom-headers

我想在 python 中向 Selenium PhantomJS 添加“自定义 header ”。 这些是我要添加的 header 。

headers = { 'Accept':'*/*',
            'Accept-Encoding':'gzip, deflate, sdch',
            'Accept-Language':'en-US,en;q=0.8',
            'Cache-Control':'max-age=0',
            'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36'
          }

这是我正在使用的代码:

from selenium import webdriver

service_args = [
    '--proxy=127.0.0.1:9999',
    '--proxy-type=socks5',
    ]
driver = webdriver.PhantomJS(service_args=service_args)


driver.set_window_size(1120, 550)
driver.get("https://duckduckgo.com/")
driver.find_element_by_id('search_form_input_homepage').send_keys("realpython")
driver.find_element_by_id("search_button_homepage").click()
print driver.current_url
driver.quit()

如何修改包含这些自定义 header 的代码?

请帮忙。

最佳答案

Andriy Ivaneyko 的方法对我不起作用(PhantomJS 2.1.1 和 Selenium 2.48.0)。

我写了一个完整的例子来设置 Selenium PhantomJS 中的所有标题、窗口大小和代理:

from selenium import webdriver

def init_phantomjs_driver(*args, **kwargs):

    headers = { 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Accept-Language':'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0',
        'Connection': 'keep-alive'
    }

    for key, value in headers.iteritems():
        webdriver.DesiredCapabilities.PHANTOMJS['phantomjs.page.customHeaders.{}'.format(key)] = value

    webdriver.DesiredCapabilities.PHANTOMJS['phantomjs.page.settings.userAgent'] = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36'

    driver =  webdriver.PhantomJS(*args, **kwargs)
    driver.set_window_size(1400,1000)

    return driver


def main():
    service_args = [
        '--proxy=127.0.0.1:9999',
        '--proxy-type=http',
        '--ignore-ssl-errors=true'
        ]

    driver = init_phantomjs_driver(service_args=service_args)

    driver.get('http://cn.bing.com')

备注1:

userAgent 设置在 phantomjs.page.settings.userAgent 而不是 phantomjs.page.customHeaders

注2:

Andriy Ivaneyko 使用enumerate 构建DesiredCapabilities.PHANTOMJS,关键是循环索引,所以数据变成:

{
 'browserName': 'phantomjs',
 'javascriptEnabled': True,
 'phantomjs.page.customHeaders.0': 'Accept-Language',
 'phantomjs.page.customHeaders.1': 'Accept-Encoding',
 'phantomjs.page.customHeaders.2': 'Accept',
 'phantomjs.page.customHeaders.3': 'User-Agent',
 'phantomjs.page.customHeaders.4': 'Connection',
 'phantomjs.page.customHeaders.5': 'Cache-Control',
 'platform': 'ANY',
 'version': ''
}

没有正确设置 header 属性。

关于python - Python 中的 Selenium PhantomJS 自定义 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35666067/

相关文章:

javascript - PhantomJS/CasperJS - 无限期地保持 session 打开并通过 HTTP 与其交互?

python - 在 tensorflow 2.0 中保存和加载模型

python - 在 Gunicorn 中使用 aiohttp 和 aiopg 时如何设置日志记录?

python - mysql-connector 没有看到 python2.7

python - 对整个复数数组进行插值

java - 具有 ID 并包含文本的 Xpath 表达式

css - 通过文本值在下拉菜单中选择一个值

python - Selenium - 网页抓取相同内容但 xpath 略有不同的多个 url

ruby - Openshift/Ruby 上的 Selenium-Webdriver 和 PhantomJS - 错误 : "Unable to find phantomjs executable"

javascript - Phantom.js : Use locally, npm已安装,jquery