Mechanize in Python - Redirect is not working after submit

元气小坏坏 提交于 2019-12-05 12:55:36

http://wwwsearch.sourceforge.net/mechanize/documentation.html

Avoid using "_http" directly. The first underscore in a name tells us that the developer was thinking on it as something private, and you probably don't need it.

In [20]: mechanize.HTTPRefreshProcessor is mechanize._http.HTTPRefreshProcessor
Out[20]: True

There are some stuff you put before opening the URL that you don't really need. For example: mechanize.Browser() isn't urllib, it already manages the cookies for you. You should not avoid robots.txt. You can follow some more "convention over configuration" by seeing before which handlers are default:

mechanize.Browser().handlers

You probably have mechanize.HTTPRedirectHandler in that list (I do), if not:

br.set_handle_redirect(mechanize.HTTPRedirectHandler)

The for loop is strange, it seems like you're changing its iterator (links inside an open URL) inside the loop (browser opens another URL). I first thought you wanted to click recursively while there's a "secure" URL match. An error would depend on how the links() generator is implemented (probably it follows a fixed br.response() instance), but I think you just want to follow the first link that match:

In [50]: br.follow_link(url_regex="secure") # No loops

I don't know what kind of redirecting/refreshing you need. JavaScript changing window.location.href? If so, mechanize won't do it, unless you parse the JavaScript yourself.

You can get the "raw" information about the last open URL this way:

last_response = br.response() # This is returned by br.open(...) too
http_header_dict = last_response.info().dict
html_string_list = last_response.readlines()
html_data = "".join(html_string_list)

Even if it's a JavaScript, you can get the redirection URL by locating it in the html_data, using html_data.find(), regular expressions, BeautifulSoup, etc..

PEP8 note: Avoid using isolated "l" (lower "L") as variable, it might be mistakenly seem as "one" or "I" (upper "i") depending on the used font and context. You should use "L" or other name instead.

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