0

I've been trying but I cant get these specific links on this page: http://www.windowsphone.com/en-us/store/top-free-apps I want to get each one of the links on the left side of this page, entertainment for example, but I cant find the right reference to get them. it's the script:

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

what should I put instead of ??? so that I cant get those links? I've tried stuff like:

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

OR

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

OR

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

etc can anyone help please?

Amin
  • 1
  • 1

1 Answers1

0

Using page.parser, you can access the underlying Nokogiri object. This allows you to use xpath to make your search.

The idea here is that all those links to have a 'data-ov' attribute that starts with 'AppLeftMerch'. This is something we can use to identify them using the 'starts-with' function.

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
Martin
  • 7,634
  • 1
  • 20
  • 23