问题
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