【问题标题】:google daily search trend scrapinggoogle每日搜索趋势抓取
【发布时间】:2019-02-22 20:39:23
【问题描述】:

我已经自学了网页抓取,并想在这里从 Google 每日搜索趋势中抓取数据:https://trends.google.com/trends/trendingsearches/daily?geo=US 这些数据将包括搜索关键词、它们的排名、每天的搜索频率。

我在开始时尝试使用 rvest 库使用 R 进行抓取,但提取命令会抓取空数据。我猜网站的 html 结构比基于 rvest 的复杂吗?所以我想学习可以应用于网站的更好方法。

我搜索了一些特定于每日搜索抓取的信息,但找不到,因为大多数帖子都涉及提取 Google 趋势数据而不是每日搜索。

从网站或更一般的此类网站中提取数据的有效方法是什么?我很高兴学习除 R 之外的任何其他工具,并且具备 Python 和 Javascript 的基本知识。如果有人可以给我一个提示,那么我会深入研究它,但目前我什至不知道从哪里开始。

谢谢,

【问题讨论】:

  • Google 趋势的优秀 R 包cran.r-project.org/web/packages/gtrendsR/gtrendsR.pdf 如果您想自己完成这项工作,请尝试使用 Selenium 网络爬虫 - 更多信息在这里 (Python) - guygw.com/2018/05/15/…
  • 我也查看了该软件包,但没有发现它有用,因为它专注于 Google 趋势而不是每日搜索?如果我遗漏了什么,请告诉我。
  • 例如,您选择的特定关键字的包提取数据,但我要提取的是每天搜索的一般关键字。
  • 我看错了这个问题,我明白了。我认为你最好的选择是设置一个 Selenium 网络爬虫。几乎每种语言都有很棒的包。

标签: python r web-scraping


【解决方案1】:

使用 firefox 中的“inpect element”工具查看 HTML。

本质上,我们可以看到你想从网页中抓取的每个元素都可以根据工具提示轻松区分:

鉴于此,我们可以使用 selenium 抓取网页以检索此信息。

(先用pip3 install -U selenium安装,再安装你喜欢的webdiverfrom the links here)

启动浏览器并使用类似于

的方式将其引导至谷歌趋势页面
╰─ ipython3
Python 3.7.0 (default, Jun 29 2018, 20:13:13)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.5.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from selenium import webdriver

In [2]: browser = webdriver.Firefox()
   ...: browser.get('https://trends.google.com/trends/trendingsearches/daily?geo=US')

你现在应该看到类似这样的东西:

再次,使用检查元素工具,获取包含要抓取的每个元素的 div 的类:

我们需要找到类名为feed-list-wrapper的div。

In [3]: list_div = browser.find_element_by_class_name("feed-list-wrapper")

In [4]: list_div
Out[4]: <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="b889702e-7e2b-7448-9180-c9fb3d1ff641", element="cad96530-3444-9d4f-a8e8-b7da780f5751")>

完成后,只需获取 div 详细信息列表:

In [5]: details_divs = list_div.find_elements_by_class_name("details")

例如,获取标题(你现在应该理解代码了)

In [6]: for detail_div in details_divs:
    ...:     print(detail_div.find_element_by_class_name("details-top").find_element_by_xpath("div/span/a").text)
    ...:
Captain Marvel
Celia Barquin Arozamena
Yom Kippur
Lethal White
National Cheeseburger Day 2018
Ind vs HK
Mario Kart
Barcelona
Emilia Clarke
Elementary
Angela Bassett
Lenny Kravitz
Lil Uzi Vert
Handmaid's Tale
Mary Poppins Returns trailer
Hannah Gadsby

另一个例子,获取观看次数:

In [7]: for detail_div in details_divs:
    ...:     title = detail_div.find_element_by_class_name("details-top").find_element_by_xpath("div/span/a").text
    ...:     search_count = detail_div.find_element_by_xpath('..').find_element_by_class_name("search-count-title").text
    ...:     print("Title : {title} \t\t\t Searchs : {search_count}".format(title=title, search_count=search_count))
    ...:
Title : Captain Marvel           Searchs : 500 k+
Title : Celia Barquin Arozamena              Searchs : 200 k+
Title : Yom Kippur           Searchs : 100 k+
Title : Lethal White             Searchs : 50 k+
Title : National Cheeseburger Day 2018           Searchs : 50 k+
Title : Ind vs HK            Searchs : 50 k+
Title : Mario Kart           Searchs : 50 k+
Title : Barcelona            Searchs : 50 k+
Title : Emilia Clarke            Searchs : 50 k+
Title : Elementary           Searchs : 20 k+
Title : Angela Bassett           Searchs : 20 k+
Title : Lenny Kravitz            Searchs : 20 k+
Title : Lil Uzi Vert             Searchs : 20 k+
Title : Handmaid's Tale              Searchs : 20 k+
Title : Mary Poppins Returns trailer             Searchs : 20 k+
Title : Hannah Gadsby            Searchs : 20 k+

你应该很快习惯硒。如果你对这里使用的方法有任何疑问,这里是selenium docs的链接

【讨论】:

  • 非常感谢您的详细解释。感谢您的回复,我有了一些想法。不过,要习惯硒还有很长的路要走。非常感谢!
  • 好吧,selenium 允许您“像人类一样”控制浏览器。你也可以点击元素(在这里,你可以点击类feed-load-more-button的div来加载更多结果,例如:browser.find_element_by_class_name('feed-load-more-button').click())
  • @Mike Remeber,几乎任何网站都可以被抓取,但有些网站比其他网站更棘手。您可以使用 selemium,如图所示,或者任何其他抓取库都可以完成这项工作(IE:beautifulsoup)。使用 selenium,如果需要,您还可以在浏览器中运行一些 javascript。
  • 感谢您的回复。我开始研究 Selenium。这真的是很大的缺失部分。非常感谢!
  • 嗨@WayToDoor,非常感谢!!这完全有道理。我会按照你的建议那样工作。干杯!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-10-07
  • 2012-05-12
  • 1970-01-01
  • 1970-01-01
  • 2022-11-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多