python - 可以设置接受语言 header 但不能设置连接 header 吗? PhantomJS(带有 Python 的 Selenium Webdriver)

标签 python selenium http-headers phantomjs

我能够设置 Accept-Language header ,但不知何故我无法将 Connection header 设置为“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>

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

最佳答案

看起来 phantomjs 连接的默认 header 是 Keep-alive,您用来查看 header 的站点不显示连接 header ,即使不使用 PhantomJS。如果您使用 Fiddler 查看您的请求,您可以看到它具有连接保持事件 header

GET/headers HTTP/1.1 Accept: 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

关于python - 可以设置接受语言 header 但不能设置连接 header 吗? PhantomJS(带有 Python 的 Selenium Webdriver),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38669108/

相关文章:

python - 如何打包 python 程序以便在网络上分发

python - scipy.weave.inline 的竞争条件

php - 为什么 curl 不在 PHP 中发送我的 header ?

json - SilverStripe如何返回JSON header

PHP/ curl : HEAD Request takes a long time on some sites

python - 从 ajax 调用 python 脚本得到 : malformed header from script. Bad header

python - 我编写的 C 扩展 Python 崩溃并显示 "Abort trap: 6"

python - 消息 : Element is disabled and so may not be used for actions error using Selenium on pythonanywhere. com

testing - 如何使用 chromedriver 测试多个版本的谷歌浏览器?

javascript - 如何处理 Protractor 中未找到元素的异常