簡體   English   中英

如何在Ruby中使用機械化提取這些特定鏈接?

[英]how to extract these specific links usuing mechanize in Ruby?

我一直在嘗試,但無法在此頁面上獲得這些特定鏈接: http : //www.windowsphone.com/en-us/store/top-free-apps我想在左側獲得每個鏈接例如,本頁面的娛樂內容 ,但是我找不到合適的參考資料來獲取它們。 它是腳本:

require 'mechanize'
agent = Mechanize.new
page = agent.get("http://www.windowsphone.com/en-us/store/top-free-apps")
page.links_with(???)

我應該放什么而不是??? 這樣我就無法獲得那些鏈接? 我已經嘗試過類似的東西:

page.links_with(:class => 'categoryNav navText')

要么

page.links_with(:class => 'categoryNav')

要么

page.links_with(:class => 'navText')

等任何人都可以幫忙嗎?

使用page.parser,您可以訪問基礎的Nokogiri對象。 這使您可以使用xpath進行搜索。

這里的想法是所有這些鏈接都具有一個以“ AppLeftMerch”開頭的“ data-ov”屬性。 我們可以使用“ starts-with ”功能來識別它們。

require 'mechanize'

agent = Mechanize.new
page = agent.get("http://www.windowsphone.com/en-us/store/top-free-apps")

page.parser.xpath("//a[starts-with(@data-ov,'AppLeftMerch')]").each do |link|
  puts link[:href]
end

暫無
暫無

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

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