python - 打开带有明确 header 的 Firefox

标签 python firefox header

我希望使用 Python 程序以编程方式打开 Firefox。问题是我想完全控制初始请求中生成的 header (所有后续 header 都可以从服务器响应中派生)。

我正在考虑通过本地代理运行初始请求并以这种方式修改请求。我不确定如何在第一个请求后“将其关闭”,或者不让 Firefox 依赖此后打开的程序。

像 TamperData 这样的东西可以在 Firefox 中实现这一点(通过手动干预),但我再次考虑以编程方式实现这一点。

最佳答案

一种选择是使用 selenium .

这个想法是用加载的 Modify Headers 来启动 Firefox。扩展并使用您要发送的自定义 header 设置其首选项。请参阅以下博客文章了解更多信息:

示例是 Java 语言,但可以适当转换为 Python。

<小时/>

请注意,如果您需要更改的唯一 header 是User-Agent - 可以通过设置 Firefox 首选项来完成,引用自 Pass the user-agent through webdriver in Selenium :

from selenium import webdriver

profile = webdriver.FirefoxProfile()
profile.set_preference("general.useragent.override","your_user_agent_string")
driver = webdriver.Firefox(profile)

关于python - 打开带有明确 header 的 Firefox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26558688/

相关文章:

python - 在线访问 Heroku Postgres 数据库

python - 在 pandas read_sql 中可选择 SQLalchemy 的方法

python - 出现错误 “FindContours support only 8uC1 and 32sC1 images in function cvStartFindContours”

firefox - FF/IE 10的ApplicationCache是​​否已完全实现(或正确实现)?

javascript - 为什么 Firefox 3 会破坏 console.log

css - 针对 Firefox Quantum 的条件 CSS 规则

python - 用第一个唯一值替换重复行的其他列并创建查找

C - Header 中的函数实现是否应该使用 extern/inline/static?

c++ - 未看到包含的头文件

css - 如何在 css 中获取窗口大小?