Python爬虫之 selenium 设置 PhantomJS header请求头

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0',
    'Connection': 'keep-alive'
}
cap = DesiredCapabilities.PHANTOMJS
for key, value in headers.items():
    cap['phantomjs.page.customHeaders.{}'.format(key)] = value
browser = webdriver.PhantomJS(desired_capabilities=cap)
browser.get("http://www.你的链接.com")
browser.quit()

打印看看 cap字典里是啥就很清楚了  ↓↓↓

{'browserName': 'phantomjs',
 'version': '',
 'platform': 'ANY',
 'javascriptEnabled': True,
 'phantomjs.page.customHeaders.Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
 'phantomjs.page.customHeaders.Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
 'phantomjs.page.customHeaders.User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0',
 'phantomjs.page.customHeaders.Connection': 'keep-alive'}
原文地址:https://www.cnblogs.com/zrzm/p/13324741.html