selenium 新增請求頭
阿新 • • 發佈:2018-11-21
參考:https://www.cnblogs.com/zhao-ying-jie/p/7082723.html
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 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4', 'Connection': 'keep-alive' } cap = DesiredCapabilities.PHANTOMJS.copy()//使用copy()防止修改原始碼定義dict for key, value in headers.items(): cap['phantomjs.page.customHeaders.{}'.format( key)] = value browser = webdriver.PhantomJS(desired_capabilities=cap)
實踐證明,如果這種方式在headers中新增cookies,是無效的。只能新增請求頭中的除cookies的其他引數。