Selenium PhantomJS custom headers in Python

前端 未结 4 1389
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-30 18:35

I want to add \"custom headers\" to Selenium PhantomJS in python. These are the headers I wanna add.

headers = { \'Accept\':\'*/*\',
            \'Accept-Encodi         


        
4条回答
  •  花落未央
    2021-01-30 19:24

    Setup headers in next way:

    from selenium import webdriver
    
    
    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'
    }
    
    for key, value in enumerate(headers):
        capability_key = 'phantomjs.page.customHeaders.{}'.format(key)
        webdriver.DesiredCapabilities.PHANTOMJS[capability_key] = value
    

    Then start work with your driver:

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

提交回复
热议问题