简体   繁体   English

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

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

I am trying to get the response codes from Mechanize in python. 我试图从python中的Mechanize获取响应代码。 While I am able to get a 200 status code anything else isn't returned (404 throws and exception and 30x is ignored). 虽然我能够获得200状态代码,但不会返回任何其他内容(404抛出异常,30x被忽略)。 Is there a way to get the original status code? 有没有办法获得原始状态代码?

Thanks 谢谢

Errors will throw an exception, so just use try:...except:... to handle them. 错误会引发异常,所以只需使用try:... except:...来处理它们。

Your Mechanize browser object has a method set_handle_redirect() that you can use to turn 30x redirection on or off. 您的Mechanize浏览器对象有一个方法set_handle_redirect(),您可以使用该方法打开或关闭30倍重定向。 Turn it off and you get an error for redirects that you handle just like you handle any other error: 关闭它,你得到的重定向错误就像处理任何其他错误一样:

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

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

twill is an outstanding automation and test layer built on top of mechanize, to make it easier to use. twill是一个出色的自动化和测试层,建立在机械化之上,使其更易于使用。 It is seriously handy. 它非常方便。

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

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