簡體   English   中英

將Mechanize寶石與Nokogirl寶石一起使用?

[英]Using the Mechanize gem with the Nokogirl gem?

我正在嘗試抓取一個需要身份驗證的網站,以獲取ID為#cellTotal的頁面上的#cellTotal

現在,使用Mechanize,我已經登錄到要訪問的頁面,但是使用了以下Nokogiri基本功能:

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

給我這個錯誤:

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

這是我到目前為止的內容:

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

如何在此頁面上選擇一個元素?

您可以使用page.parser獲得對基礎Nokogiri對象的訪問。

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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM