繁体   English   中英

使用Python中的Mechanize获取和捕获HTTP响应

[英]Getting and trapping HTTP response using Mechanize in Python

我试图从python中的Mechanize获取响应代码。 虽然我能够获得200状态代码,但不会返回任何其他内容(404抛出异常,30x被忽略)。 有没有办法获得原始状态代码?

谢谢

错误会引发异常,所以只需使用try:... except:...来处理它们。

您的Mechanize浏览器对象有一个方法set_handle_redirect(),您可以使用该方法打开或关闭30倍重定向。 关闭它,你得到的重定向错误就像处理任何其他错误一样:

>>> from mechanize import Browser
>>> browser = Browser()
>>> resp = browser.open('http://www.oxfam.com') # this generates a redirect
>>> resp.geturl()
'http://www.oxfam.org/'
>>> browser.set_handle_redirect(False)
>>> resp = browser.open('http://www.oxfam.com')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build\bdist.win32\egg\mechanize\_mechanize.py", line 209, in open
  File "build\bdist.win32\egg\mechanize\_mechanize.py", line 261, in _mech_open
mechanize._response.httperror_seek_wrapper: HTTP Error 301: Moved Permanently
>>>
>>> from urllib2 import HTTPError
>>> try:
...    resp = browser.open('http://www.oxfam.com')
... except HTTPError, e:
...    print "Got error code", e.code
...
Got error code 301

在twill中,执行get_browser().get_code()

twill是一个出色的自动化和测试层,建立在机械化之上,使其更易于使用。 它非常方便。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM