【问题标题】:Can set accept-language header but not Connection header? PhantomJS (Selenium Webdriver with Python)可以设置接受语言标头但不能设置连接标头吗? PhantomJS(带有 Python 的 Selenium Webdriver)
【发布时间】:2016-07-30 00:08:38
【问题描述】:

我可以设置 Accept-Language 标头,但不知何故我无法将 Connection 标头设置为“keep-alive”:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import     DesiredCapabilities

webdriver.DesiredCapabilities.PHANTOMJS['phantomjs.page.customHeaders.Accept-Language'] = 'ru-RU'

webdriver.DesiredCapabilities.PHANTOMJS['phantomjs.page.customHeaders.Connection'] = "keep-alive"

driver = webdriver.PhantomJS("/home/user/bin/phantomjs",service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any'])

driver.set_window_size(1120, 550)

driver.get("http://www.httpbin.org/headers")

print(driver.page_source)

输出

<html><head></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">{
  "headers": {
    "Accept":     "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "ru-RU",
    "Host": "www.httpbin.org",
    "User-Agent": "Mozilla/5.0 (Unknown; Linux x86_64) AppleWebKit/538.1   (KHTML, like Gecko) PhantomJS/2.1.1 Safari/538.1"
  }
}
</pre></body></html>

我认为,无论出于何种原因,标头本身或字段都区分大小写,因此我查找了这些标头的示例并按原样使用它们,但没有骰子。如何设置 Connection 标头或 Keep-alive 标头?

【问题讨论】:

    标签: python selenium http-headers phantomjs


    【解决方案1】:

    看起来 phantomjs 连接的默认标头是 Keep-alive,您用于查看标头的站点不显示 Connection 标头,即使不使用 PhantomJS。如果您使用 Fiddler 查看您的请求,您会看到它具有连接保持活动标头

    GET /headers HTTP/1.1 接受:text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
    用户代理:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.1.1 Safari/538.1

    连接:保持活动状态

    接受编码:gzip、deflate 接受语言:en-US,* 主办方:www.httpbin.org

    【讨论】:

      猜你喜欢
      • 2011-12-11
      • 2023-02-11
      • 2014-01-01
      • 2015-06-23
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      • 2014-06-02
      • 1970-01-01
      相关资源
      最近更新 更多