How can I perform a HEAD request with the mechanize library?

孤者浪人 提交于 2019-12-19 09:19:34

问题


I know how to do a HEAD request with httplib, but I have to use mechanize for this site.

Essentially, what I need to do is grab a value from the header (filename) without actually downloading the file.

Any suggestions how I could accomplish this?


回答1:


Mechanize itself only sends GETs and POSTs, but you can easily extend the Request class to send HEAD. Example:

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()



回答2:


In mechanize there is no need to do HeadRequest class etc.

Simply


import mechanize

br = mechanize.Browser()

r = br.open("http://www.example.com/")

print r.info()

That's all.



来源:https://stackoverflow.com/questions/137580/how-can-i-perform-a-head-request-with-the-mechanize-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!