【问题标题】:How do I parse Google search results with Nokogiri?如何使用 Nokogiri 解析 Google 搜索结果?
【发布时间】:2023-03-29 05:40:01
【问题描述】:

我需要帮助从 Google 搜索结果中提取 URL,并被告知使用 Nokogiri。我安装了它并阅读了 Nokogiri 文档,但不知道从哪里开始——对我来说都是希腊文。

我知道我要查找的是每个结果的 URL,每个结果都存在于 <cite> 标记之间。到目前为止,我所能做的就是提取搜索结果,但我只是不知道如何从文件中提取特定数据。这是我确实拥有的一小段代码:

serp = Nokogiri::HTML(open("http://www.google.com/search?num=100&q=stackoverflow"))

【问题讨论】:

  • 调查 Nokogiri 对 CSS 访问器的使用。它们非常强大,可以帮助您快速滚动。从那里您需要深入研究 XPath,因为这就是我们经常追踪节点的方式,无论它们是 HTML 还是 XML。 XPath 比 CSS 强大得多,但这种强大也带来了复杂性。另外,作为一个可用性提示,at 找到第一次出现的东西为 Nodesearch 找到所有出现的地方,返回 NodeSet。 NodeSet 就像一个节点数组,因此您可以对其进行迭代。

标签: html ruby parsing nokogiri


【解决方案1】:

享受:)

require 'open-uri'
require 'nokogiri'

page = open "http://www.google.com/search?num=100&q=stackoverflow"
html = Nokogiri::HTML page

html.search("cite").each do |cite|
  puts cite.inner_text
end

也看nokogiri tutorials

【讨论】:

  • 不是恢复旧帖子,但您知道是否有一种现代方法可以控制 Google 结果的结果数量? num 查询字符串不再起作用。
  • @DaveLong 它对我有用,但我认为有 100 个结果的硬性限制
  • 这个好像不行了,google不喜欢狂野解析
  • 虽然这个方法有效,但我最近在尝试上述方法后发现,每天只能查询 100 个查询。一种更具可扩展性的方法是使用 Google 的自定义搜索 API。我在这个相关问题中写了一个完整的例子和答案。 stackoverflow.com/a/34970424/3858363
【解决方案2】:

确保您使用的是user-agent(标头),否则它将返回空输出,因为 Google 最终会阻止请求。 What is my user-agent.

headers = {
  "User-Agent" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582"
}

代码和example in the online IDE

require 'nokogiri'
require 'httparty'
require 'json'

headers = {
  "User-Agent" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582"
}

params = {
  q: "stackoverflow",
  num: "100"
}

response = HTTParty.get("https://www.google.com/search",
                        query: params,
                        headers: headers)
doc = Nokogiri::HTML(response.body)


data = doc.css(".tF2Cxc").map do |result|
  title = result.at_css(".DKV0Md")&.text
  link = result.at_css(".yuRUbf a")&.attr("href")
  displayed_link = result.at_css(".tjvcx")&.text
  snippet = result.at_css(".VwiC3b")&.text
  # puts "#{title}#{snippet}#{link}#{displayed_link}\n\n"

  {
    title: title,
    link: link,
    displayed_link: displayed_link,
    snippet: snippet,
  }.compact
end

puts JSON.pretty_generate(data)

--------
=begin
[
  {
    "title": "Stack for Stack Overflow - Apps on Google Play",
    "link": "https://play.google.com/store/apps/details?id=me.tylerbwong.stack&hl=en_US&gl=US",
    "displayed_link": "https://play.google.com › store › apps › details",
    "snippet": "Stack is powered by Stack Overflow and other Stack Exchange sites. Search and filter through questions to find the exact answer you're looking for!"
  }
...
]
=end

或者,您可以从 SerpApi 中 Google Organic Results API。这是一个带有免费计划的付费 API。

主要区别在于无需弄清楚如何抓取页面的某些部分。所需要做的只是迭代结构化的 JSON 字符串。

require 'google_search_results' 
require 'json'

params = {
  api_key: ENV["API_KEY"],
  engine: "google",
  q: "stackoverflow",
  hl: "en",
  num: "100"
}

search = GoogleSearch.new(params)
hash_results = search.get_hash

data = hash_results[:organic_results].map do |result|
  title = result[:title]
  link = result[:link]
  displayed_link = result[:displayed_link]
  snippet = result[:snippet]

  {
    title: title,
    link: link,
    displayed_link: displayed_link,
    snippet: snippet
  }.compact
end

  puts JSON.pretty_generate(data)


-------------
=begin
[
  {
    "title": "Stack Overflow - Home | Facebook",
    "link": "https://www.facebook.com/officialstackoverflow/",
    "displayed_link": "https://www.facebook.com › Pages › Interest",
    "snippet": "Stack Overflow. 519455 likes · 587 talking about this. We are the world's programmer community."
  }
...
]
=end

免责声明,我为 SerpApi 工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-06
    • 1970-01-01
    • 2020-06-19
    • 1970-01-01
    • 2010-12-05
    • 2014-03-08
    • 1970-01-01
    • 2021-08-23
    相关资源
    最近更新 更多