Mechanize - 用Python實現有狀態的可編程Web瀏覽器

碼頭工人 13年前發布 | 21K 次閱讀 Python

Mechanize提供了一個有狀態的可編程Web瀏覽器。它支持HTML表單填充,自定義Headers,HTTP認證,和SSL支持等。

項目地址: http://wwwsearch.sourceforge.net/mechanize/

import re
import mechanize

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

follow second link with element text matching regular expression

response1 = br.follow_link(text_regex=r"cheese\s*shop", nr=1) assert br.viewing_html() print br.title() print response1.geturl() print response1.info() # headers print response1.read() # body

br.select_form(name="order")

Browser passes through unknown attributes (including methods)

to the selected HTMLForm.

br["cheeses"] = ["mozzarella", "caerphilly"] # (the method here is setitem)

Submit current form. Browser calls .close() on the current response on

navigation, so this closes response1

response2 = br.submit()

print currently selected form (don't call .submit() on this, use br.submit())

print br.form

response3 = br.back() # back to cheese shop (same data as response1)

the history mechanism returns cached response objects

we can still use the response, even though it was .close()d

response3.get_data() # like .seek(0) followed by .read() response4 = br.reload() # fetches from server

for form in br.forms(): print form

.links() optionally accepts the keyword args of .follow_/.find_link()

for link in br.links(url_regex="python.org"): print link br.follow_link(link) # takes EITHER Link instance OR keyword args br.back()</pre>

 本文由用戶 碼頭工人 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!