机械化 page.search 不适用于 link.click 方法
Mechanize page.search does not work with link.click method
我有一个商店页面,想先将所有文章作为 link 查看。之后想爬取每个link的内容。我得到的 links 如下:
agent = Mechanize.new
page = agent.get(page_URL)
article_links = page.search('div.sklep-produkt h3 a').attr('href')
我使用 CSS 进行搜索,因为这是我搜索确切标签或区域的最简单方法。如果我输入 puts "#{article_links}"
,我已经可以看到 link,所以它可以正常工作。但是,我想要不止一篇 link - 所有文章 links - 来自页面所以我使用这个:
article_links.uniq { |link| link.uri }.map do |link| #no double entries
link.click
target_URL = page.uri + link.uri
puts "#{target_URL}"
end
问题是 uniq、map、link.click、uri 等方法不适用于 page.search。示例错误:
undefined method `uri' for #<Nokogiri::XML::Attr:0x0055a7a4a7e440> (NoMethodError)
只有 page.links_with(...)
才有可能。
如何使用 page.search
抓取多个 link?
类似...
# find the links
article_links = page.css('div.sklep-produkt').css('h3').css('a')
# store in a new array, and take out the un-uniques
unique_article_links = article_links.map {|l| l.attribute('href').value }.uniq
# visit each link and do whatever needs to be done
unique_article_links.each do |link|
agent.get(link) do |l|
#... do stuff here
p l.css('title').text
end
end
我在维基百科上对此进行了测试,它返回的标题对我来说还不错:
url = 'https://en.wikipedia.org/wiki/Main_Page'
agent = Mechanize.new
page = agent.get(url)
article_links = page.css('div#mp-tfa').css('a')
unique_article_links = article_links.map {|l| l.attribute('href').value }.uniq
unique_article_links.each do |link|
agent.get(link) do |l|
#... do stuff here
p l.css('title').text
end
end
发生的事情是它返回一个 Nokogiri object 而不是一个数组,所以你必须从 Nokogiri object 中获取你想要的值并放入一个新数组中,然后你可以使用数组方法。
我有一个商店页面,想先将所有文章作为 link 查看。之后想爬取每个link的内容。我得到的 links 如下:
agent = Mechanize.new
page = agent.get(page_URL)
article_links = page.search('div.sklep-produkt h3 a').attr('href')
我使用 CSS 进行搜索,因为这是我搜索确切标签或区域的最简单方法。如果我输入 puts "#{article_links}"
,我已经可以看到 link,所以它可以正常工作。但是,我想要不止一篇 link - 所有文章 links - 来自页面所以我使用这个:
article_links.uniq { |link| link.uri }.map do |link| #no double entries
link.click
target_URL = page.uri + link.uri
puts "#{target_URL}"
end
问题是 uniq、map、link.click、uri 等方法不适用于 page.search。示例错误:
undefined method `uri' for #<Nokogiri::XML::Attr:0x0055a7a4a7e440> (NoMethodError)
只有 page.links_with(...)
才有可能。
如何使用 page.search
抓取多个 link?
类似...
# find the links
article_links = page.css('div.sklep-produkt').css('h3').css('a')
# store in a new array, and take out the un-uniques
unique_article_links = article_links.map {|l| l.attribute('href').value }.uniq
# visit each link and do whatever needs to be done
unique_article_links.each do |link|
agent.get(link) do |l|
#... do stuff here
p l.css('title').text
end
end
我在维基百科上对此进行了测试,它返回的标题对我来说还不错:
url = 'https://en.wikipedia.org/wiki/Main_Page'
agent = Mechanize.new
page = agent.get(url)
article_links = page.css('div#mp-tfa').css('a')
unique_article_links = article_links.map {|l| l.attribute('href').value }.uniq
unique_article_links.each do |link|
agent.get(link) do |l|
#... do stuff here
p l.css('title').text
end
end
发生的事情是它返回一个 Nokogiri object 而不是一个数组,所以你必须从 Nokogiri object 中获取你想要的值并放入一个新数组中,然后你可以使用数组方法。