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'}
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Python爬虫之 selenium 设置 PhantomJS header请求头 - Python技术站