Using the Mechanize gem with the Nokogirl gem?

雨燕双飞 提交于 2019-12-11 19:52:34

问题


I'm trying to scrape a website that requires authentication to get an element on a page with an id of #cellTotal.

Right now, using Mechanize I have logged into the page I want to access, but using basic Nokogiri functions like:

@selector = page.css("#cellTotal").text

Gives me this error:

undefined method `css' for #<Mechanize::Page:0x61234f8>

Here is what I have so far:

agent = Mechanize.new
agent.get("example.com")
agent.page.forms[0]["username_field"] = "username"
agent.page.forms[0]["password_field"] = "password"
agent.page.forms[0].submit

@selector = agent.page.css("#cellTotal").text

How can I select an element on this page?


回答1:


You can use page.parser to gain access to the underlying Nokogiri object.

http://mechanize.rubyforge.org/Mechanize/Page.html#method-i-parser

require 'mechanize'
agent = Mechanize.new
agent.get("http://stackoverflow.com/questions/23064821/using-the-mechanize-gem-with-the-nokogirl-gem/")
agent.page.parser.class
# => Nokogiri::HTML::Document
agent.page.parser.css("#answer-23065003 .user-details a").text
# => "akatakritos"


来源:https://stackoverflow.com/questions/23064821/using-the-mechanize-gem-with-the-nokogirl-gem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!