Python PhantomJS customHeader更新不反映si

2024-09-30 22:24:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图通过phantomJS中的customHeader传递某些ip,以显示基于ip的各种评级徽标。我第一次将头设置为特定的ip时,页面会反映ip(正确的图像加载)。当我尝试修改customHeader并加载另一个页面时,该页面反映了旧的ip。你知道吗

#loop through passing different ip's
    if self.phantom is not None:
        #These seem to do nothing to fix the issue
        #self.phantom.get('javascript:localStorage.clear();')
        #self.phantom.delete_all_cookies()
        self.phantom.close()
        self.phantom.quit()
        self.phantom = None

    service = ['--ignore-ssl-errors=true', '--disk-cache=false']
    dcaps = {'handlesAlerts': True, 'javascriptEnabled': True}
    dcaps['phantomjs.page.settings.userAgent'] = (
            "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/53 "
            "(KHTML, like Gecko) Chrome/15.0.87")

    #No matter if i set customHeader during creation or after i get same result
    #if custom_header_name is not None and custom_header_value is not None:
    #    dcaps['phantomjs.page.customHeaders.{0}'.format(custom_header_name)]=custom_header_value

    self.phantom = webdriver.PhantomJS(executable_path=self.config.settings['phantom_js_dir'], desired_capabilities=dcaps, service_args=service)

    self.phantom.desired_capabilities[u'phantomjs.page.customHeaders.{0}'.format(custom_header_name)] = custom_header_value

    self.phantom.get(self.url)
    self.phantom.save_screenshot("C:\\test.png")

我尝试过清除cookies、缓存和重置customHeader,但是只有第一个ip等级徽标呈现,即使当我观察customHeader值时,它会在整个运行过程中更改(以更正值)。你知道吗

更新:http://pastebin.com/uAm4Sk3R(更深入的代码)


Tags: selfipnonegetifisservicecustom