Вопрос

I am trying to scrape a page, and need to click on some links within a menu. If I use the search method, I am then stuck with a Nokogiri object, and therefore can not use the click method.

agent.page.search('.right-menu').links_with(href: /^\/blabla\//).each do |link|
  region = link.click
end

The following would tell me that links_with is not defined. How can I make a select links from a specific menu? Is there a way I can parse the object back to a Mechanize object?

Это было полезно?

Решение

You can try something like this:

agent.page.search('.youarehere > a').each do |a|
  link = Mechanize::Page::Link.new(a.attr('href'), agent, agent.page)
  region = link.click
end

not the cleanest way to do it I guess, but Mechanize is doing almost the same in its source code: http://mechanize.rubyforge.org/Mechanize/Page.html#method-i-links

Would be a nice addition though, instead of going through this.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top