python - 如何使用 mechanize 库执行 HEAD 请求?

标签 python http-headers mechanize

我知道如何使用 httplib 执行 HEAD 请求,但我必须对该站点使用 mechanize。

本质上,我需要做的是从 header (文件名)中获取一个值,而无需实际下载文件。

有什么建议可以实现吗?

最佳答案

Mechanize 本身只发送 GET 和 POST,但您可以轻松扩展 Request 类以发送 HEAD。示例:

import mechanize

class HeadRequest(mechanize.Request):
    def get_method(self):
        return "HEAD"

request = HeadRequest("http://www.example.com/")
response = mechanize.urlopen(request)

print response.info()

关于python - 如何使用 mechanize 库执行 HEAD 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/137580/

相关文章:

Ruby Mechanize 登录不工作

python - 类型错误 : 'TextBlob' object is not callable

python - WSL 上的 Virtualenv 创建的文件与 PowerShell 上的 Virtualenv 不同

html - 响应头 VS 元标签

http - Content-Range 和 Range header 之间的区别?

ruby - 使用Selenium-driver的描述中提到的代码可以在 Mechanize 中实现吗?

python - def __init__(self) 的用处?

Python - 在字符串中查找字符串列表的出现

java - 如何在 Glassfish 服务器中设置 HTTP header

ruby - 无法安装 gem Mechanize