繁体   English   中英

与watir + phantomjs一起使用机械化

[英]Using mechanize with watir + phantomjs

我正在尝试将幻影js生成的html插入机械化对象中,以便我可以轻松地进行搜索。 我尝试了以下无济于事...

b = Watir::Browser.new :phantomjs
url = "www.google.com"
b.goto url

agent = Mechanize.new
#Following is not executed at same time...
#Error 1: lots of errors
page = agent.get(b.html)

#Error 2: `parse': wrong number of arguments (1 for 3) (ArgumentError)
page = agent.parse(b.html)

#Error 3 last ditch effort: undefined method `agent' 
page = agent(b.html)

:我觉得通过我开始怀疑我是否能机械化现有的HTML对象......我最初通过骑上它http://shane.in/2014/01/headless-web-scraping/HTTP: //watirmelon.com/2013/02/05/watir-webdriver-with-ghostdriver-on-osx-headless-browser-testing/

我当时的情况也一样。 我写了很多与机械化代码,因此,我不想要移动到nokogiri使用时watir 下面的代码是我的工作方式。

require 'watir'
require 'mechanize'

b = Watir::Browser.new
b.goto(url)
html = b.html
a = Mechanize.new
page = Mechanize::Page.new(nil, {'content-type'=>'text/html'}, html, nil, a)

您可以使用页面来搜索元素。

require 'watir'
require 'nokogiri'

b = Watir::Browser.new :phantomjs
url = "http://google.com"
b.goto url

p Nokogiri::HTML(b.html)

您最好使用Nokogiri进行此操作(也就是说,如果只需要在源代码中搜索一些数据)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM