微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

mechanize Python的HTML解析

程序名称:mechanize

授权协议: BSD

操作系统: Linux

开发语言: Python

mechanize 介绍

当您希望与 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

mechanize 官网

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐