Skip to main content
 首页 » 编程设计

python之谷歌每日搜索趋势抓取

2025年05月04日17zlslch

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

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

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

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

谢谢,

请您参考如下方法:

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

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

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

(首先使用 pip3 install -U selenium 安装它并安装你最喜欢的 webdiver from the links here )

启动浏览器并使用类似于

的内容将其定向到 google trends 页面
╰─ 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。如果您对这里使用的方法有任何疑问,这里有一个链接到 selenium docs