可以设置accept-language标头而不是Connection标头?PhantomJS(Python的Selenium Webdriver)

not*_*ain 6 python selenium http-headers phantomjs

我可以设置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)
Run Code Online (Sandbox Code Playgroud)

输出:

<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>
Run Code Online (Sandbox Code Playgroud)

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

小智 2

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

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) ,如 Gecko)PhantomJS/2.1.1 Safari/538.1

连接:保持活动状态

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