本文介绍了将Mechanize宝石与Nokogirl宝石一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试抓取一个需要身份验证的网站,以获取ID为#cellTotal的页面上的元素.

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

现在,使用Mechanize,我已经登录到要访问的页面,但是使用了以下基本的Nokogiri功能:

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

给我这个错误:

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?

推荐答案

您可以使用page.parser获得对基础Nokogiri对象的访问.

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"

这篇关于将Mechanize宝石与Nokogirl宝石一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 03:45