Python下Selenium PhantomJs设置header的方法

Python下Selenium PhantomJs设置header的方法

导入所需的模块:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

设置Headers:

    cookies = “_trs_uv=k75j1tzf_6_85hv; u=1; JSESSIONID=331D57E45CA390FBDC01615B8503CD0B; experience=show”
    
    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 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36',
        'Connection': 'keep-alive',
        "Cookie": cookies
    }

将headers写入phantomjs 的customHeaders

    cap = DesiredCapabilities.PHANTOMJS.copy()  # 使用copy()防止修改原代码定义dict

    for key, value in headers.items():
        cap['phantomjs.page.customHeaders.{}'.format(
            key)] = value

最后,可以访问网页了

    driver = webdriver.PhantomJS(executable_path=/xxxx/xxxx/phantomjs/bin/phantomjs”, desired_capabilities=cap)
    
    driver.get("https://www.baidu.com/")

转载于: https://my.oschina.net/u/2396236/blog/1790714.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值