当您希望与 Web 页面中找到的内容进行某种比较复杂的交互时,您需要使用 mechanize

示例代码:

import re
from mechanize import Browser

br = 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
response1.close()  # (shown for clarIty; in fact Browser does this for you)

br.select_form(name="oRDEr")
# Browser passes through unknown attributes (including methods)
# to the selected HTMLForm (from ClientForm).
br["cheeses"] = ["mozzarella", "caerphilly"]  # (the method here is __setitem__)
response2 = br.submit()  # submit current form

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

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务