Python, Mechanize ,设置多个 header 的正确语法?

标签 python http-headers mechanize webautomation

我似乎找不到如何做到这一点,我正在尝试使用 python 的 mechanize 模块设置多个 header ,例如:

br.addheaders = [('user-agent', '   Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3')]
br.addheaders = [('accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')]

但它似乎只需要最后一个 br.addheaders.. 所以它只显示“accept” header ,而不是“user-agent” header ,这让我相信每次调用“br.addheaders”覆盖之前对此的任何调用。我无法理解包含 2 个或更多 header 的语法,所以我将不胜感激任何帮助。

我正在使用这个网站来测试标题输出:

http://www.ericgiguere.com/tools/http-header-viewer.html

最佳答案

根据 http://wwwsearch.sourceforge.net/mechanize/doc.html#adding-headers , 语法为

br.addheaders = [('user-agent', '   Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3'),
('accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')]

也就是说,制作一个标题元组列表。

关于Python, Mechanize ,设置多个 header 的正确语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3564509/

相关文章:

python - 在 Node.js 服务器上运行 Python 脚本时出错

java - 在 Struts 中将 JSON 字符串写入 Printwriter 时出现 ArrayIndexOutOfBoundsException

ios - 如何在 UIWebView 请求中添加自定义 HTTP header ,我的 UIWebView 是基于 Cordova 项目的?

c# - 为什么这个 UTF-16 HTTP 响应在生成的流中最终变成 UTF-8?

python - Pyparsing:嵌套 Markdown 强调

python - 更改文件名?

python - 效率与可读性 : obfuscation when using nested boolean index arrays

Python Mechanize 写入 TinyMCE 文本编辑器

Python Mechanize 文件下载

python - 用 Python 构建 Web 浏览器和有关 cookie 的问题