与网页交互(Interacting With a Web Page)
通过Mechanize模拟自然的浏览器行为来完成与网页之间的交互.
# Mechanize的安装
Mechanize的安装请查看 -> Ruby library的安装
# 使用Mechanize时,需要在脚本头部注明
require 'mechanize'
# Mechanize object的设定
agent = WWW::Mechanize.new
# 对虚拟的user_agent的设定
agent.user_agent_alias = 'Windows IE 7'
# 历史纪录的大小设
agent.max_history = 1
# 激活网址
agent.get('http://www.test.com')
# 网页连接超时设定
agent.open_timeout = 10
# 设定连接网页时的认证(要在激活网页前使用)
agent.auth('username', 'password')
# 读取网页的title标签
page = agent.get('http://www.test.com')
page.title
# 取得当前网页的网址
page = agent.get('http://www.test.com')
page.uri
# 读取网页的form标签
page.forms.name('Name')
# 使用form的action
page.forms.action('test.php')
# 如果网页中有多个form时
page.forms[0]
# 如果网页中有多个同名称或相同action的form时
page.forms.name('Name').first
# 使用传递方法进行判断
page.forms.action('./test.php').find{|f| f.method == 'POST'}
# 文本框,密码框,文本域的填写
form = page.forms.name('Name').first
form['文本框或文本域name'] = '填写内容'
form.fields.name('文本框或文本域name').value = '填写内容'
# submit标签
form = page.form[0]
form.submit
form.submit(form.buttons.name('Name'))
# CheckBox标签
form = page.forms.name('chkbox')
返回的值为 c1 = form.checkboxes[0].name
返回的值为 val2 = form.checkboxes[1].value
返回的值为 false = form.checkboxes.name("c1″).checked
# 复选框的选择
form.checkboxes.name('c1').check
# 对应的HTML代码为:
<form name="chkbox">
<input type="checkbox" name="c1″ value="val1″>复选框1
<br>
<input type="checkbox" name="c2″ value="val2″ checked>复选框2
</form>
# 单选框的选择
form.radiobuttons.name('box')[1].check
# 获得当前SelectList选择的选项及SelectList的设定
form = page.forms.name(form1).first
返回的值为 選択3 = form.fields.name('select1').value
返回的值为 選択3 = form.fields.name('select1').options[2].select
# 选项的选择
form.name('select1').value = “1″
对应的HTML代码为:
<form name='form1' action="" method="post">
<select name='select1'>
<option value="1″>選択1</option>
<option value="2″>選択2</option>
<option value="3″ selected>選択3</option>
</select>
</form>
# SelectList多选择时为
form.fields.name('select1') = ['one', 'three']
# 对应的HTML代码为:
<form>
<select name="select1″ size="3″ multiple>
<option value="one" selected>選択1</option>
<option value="two">選択2</option>
<option value="three" selected>選択3</option>
</select>
</form>
# 文件的上传
# 方法一
page.forms[0].file_uploads.name('upfile').file_name = './hoge.jpg'
# 方法二
page.forms[0].file_uploads.name('upfile').file_data = File.open('./hoge.jpg','rb'){|f| f.read}
# 对应的HTML代码为:
<form>
<input type="file" name="upfile" enctype="multipart/form-data">
</form>
# 网页中的超链接
# 网页中的全部超链接的输出
page = agent.get('http://www.test.com')
page.links.each{|link| puts link.href}
# 网页中超链接的击活(可使用正则表达式)
page.links.href('http://www.test.com')[2].click
page.links.text('文本')[0].click
# frame网页的查看
# 方法一:使用name查看
page.frames.name('left').click
# 方法二:使用链接查看
leftpage = page.frames.src('frame_left.htm').click
# 对应的HTML代码为:
<frameset cols="150,*">
<frame src="frame_left.htm" name="left">
<frame src="frame_right.htm" name="right">
</frameset>
# 网页中的文件下载(正则表达式可用)
agent = WWW::Mechanize.new
data = agent.get_file('http://www.test.com/top.gif')
open('top.gif', 'wb'){|f| f.write(data)}
# 对应的HTML代码为:
<img src="http://www.test.com/top.gif">
# table部分的的文字
page.root.search('table/tr/td'){|e| puts e}
ruby
分享到:
相关推荐
ruby mechanize包,mechanize-2.7.3.gem
Mechanize 一个让自动化web交互变得容易的ruby库
ruby mechanize 2.7.2 gem 包
Mechanize相关资料
multi-mechanize-master
ruby mechanize安装需要的gem包,安装需要的本地gem文件
Mechanize¶↑docs.seattlerb.org/mechanize github.com/sparklemotion/mechanize描述¶↑Mechanize库用于自动与网站交互。 Mechanize自动存储和发送coo Mechanize¶↑docs.seattlerb.org/mechanize github....
用python成为顶级黑客,第三方库必备,mechanize网络爬虫
机械化描述Mechanize库用于自动与网站进行交互。 Mechanize自动存储和发送Cookie,遵循重定向,并可以遵循链接和提交表单。 可以填写和提交表单字段。 机械化还可以将您曾经访问过的站点作为历史记录进行跟踪。依存...
Mechanize自动存储和发送Cookie,跟随重定向,跟随链接并提交表单。 可以填写和提交表单字段。 机械化还可以将您曾经访问过的站点作为历史记录进行跟踪。入门在根文件夹中,您可以运行get_page示例: node examples...
mechanize是对urllib2的部分功能的替换,能够更好的模拟浏览器行为,在web访问控制方面做得更全面。结合beautifulsoup和re模块,可以有效的解析web页面,我比较喜欢这种方法。 下面主要总结了使用mechanize模拟...
mechanize-源码.rar
Ruby中实现网页抓取,一般用的是mechanize,使用非常简单。 安装 代码如下: sudo gem install mechanize 抓取网页 代码如下: require ‘rubygems’ require ‘mechanize’ agent = Mechanize.new page = agent.get...
python mechanize可以方便的模拟URL的访问,测试时的必要工具。
每次我需要做任何关于web的自动任务时,我都使用这段python代码去模拟一个浏览器。 import mechanize import cookielib # Browser br = mechanize.Browser() # Cookie Jar cj = cookielib.LWPCookieJar() br....
WWW-Mechanize-Chrome:自动化Chrome浏览器
# Browserbr = mechanize.Browser()br.set_cookiejar(cj) # Browser optionsbr.set_handle_equiv(True)br.set_handle_gzip(True)br.set_handle_redirect(True)br.set_handle_referer(True)br.se
You’ll find out how to automate these actions with Python packages such as mechanize. You’ll also learn how to create class-based scrapers with Scrapy libraries and implement your learning on real ...