加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 我们可以一起使用selenium-webdriver和nokogiri吗?

发布时间:2020-12-16 21:53:17 所属栏目:百科 来源:网络整理
导读:我用Nokogiri如下: require 'nokogiri'require 'open-uri'# Get a Nokogiri::HTML::Document for the page we’re interested in...doc = Nokogiri::HTML(open('http://www.google.com/search?q=sparklemotion')) 但我的不好,由于公司防火墙可能,我收到错误
我用Nokogiri如下:
require 'nokogiri'
require 'open-uri'

# Get a Nokogiri::HTML::Document for the page we’re interested in...

doc = Nokogiri::HTML(open('http://www.google.com/search?q=sparklemotion'))

但我的不好,由于公司防火墙可能,我收到错误:

C:/Ruby193/lib/ruby/1.9.1/net/http.rb:762:in `initialize': getaddrinfo: No such
host is known.  (SocketError)

因此,我认为我会使用selenium-webdriver导航和nokogiri在网页源html上工作.

require "rubygems"
require "selenium-webdriver"

driver = Selenium::WebDriver.for :firefox
driver.get "http://www.google.com/search?q=sparklemotion"

那么我在这里如何向nokogiri提供网页内容(html)?

请在这里建议我.

解决方法

您可以使用 page_source方法从selenium-webdriver获取页面源:
driver.page_source

所以你的脚本可能是:

require 'selenium-webdriver'
require 'nokogiri'

driver = Selenium::WebDriver.for :firefox
driver.get "http://www.google.com/"

doc = Nokogiri::HTML(driver.page_source)
# Do whatever with nokogiri

也就是说,我不知道你为什么要使用nokogiri而不是selenium-webdriver.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读