无需包装器或使用 API:Python 即可访问 Google 趋势数据

har*_*rry 6 python beautifulsoup request web-scraping google-trends

我正在尝试编写一个 Python 程序来从 Google 趋势 (GT) 中收集数据 - 具体来说,我想自动打开 URL 并访问折线图中显示的特定值:

在此处输入图片说明

我会很高兴下载 CSV 文件,或者通过网络抓取值(根据我对 Inspect Element 的阅读,清理数据只需要一两个简单的拆分)。我要进行许多搜索(许多不同的关键字)

我正在创建许多 URL 来从 Google 趋势中收集数据。我使用了测试搜索中的实际 URL。URL 示例:https : //trends.google.com/trends/explore?q= sports%20cars &geo= US 在浏览器上实际搜索此 URL 会显示相关的 GT 页面。当我尝试通过程序访问它时,问题就出现了。

我看到的大多数回复都建议使用来自 Pip 的公共模块(例如 PyTrends 和“非官方 Google Trends API”)——我的项目经理坚持我不使用不是由站点直接创建的模块(即:API 是可以接受的,但仅限于官方的 Google API)。只有 BeautifulSoup 被批准为插件(不要问为什么)。

下面是我尝试过的代码示例。我知道这是基本的,但是在我收到的第一个请求中:

HTTPError:HTTP 错误 429:未知”:请求过多。

对其他问题的一些回答提到了 Google Trends API - 这是真的吗?我在官方 API 上找不到任何文档。

这是另一篇文章,其中概述了我尝试过但对我不起作用的解决方案:

https://codereview.stackexchange.com/questions/208277/web-scraping-google-trends-in-python

url = 'https://trends.google.com/trends/explore?q=sports%20cars&geo=US'

html = urlopen(url).read()

soup = bs(html, 'html.parser')

divs = soup.find_all('div')

return divs
Run Code Online (Sandbox Code Playgroud)

QHa*_*arr 6

它使用您可以在网络选项卡中找到的 API

import requests
import json

r = requests.get('https://trends.google.com/trends/api/widgetdata/multiline?hl=en-GB&tz=-60&req=%7B%22time%22:%222018-05-29+2019-05-29%22,%22resolution%22:%22WEEK%22,%22locale%22:%22en-GB%22,%22comparisonItem%22:%5B%7B%22geo%22:%7B%22country%22:%22US%22%7D,%22complexKeywordsRestriction%22:%7B%22keyword%22:%5B%7B%22type%22:%22BROAD%22,%22value%22:%22sports+cars%22%7D%5D%7D%7D%5D,%22requestOptions%22:%7B%22property%22:%22%22,%22backend%22:%22IZG%22,%22category%22:0%7D%7D&token=APP6_UEAAAAAXO-yaYekqJ7Tf2nuoLBAigMSW7axoLTL&tz=-60')
data = json.loads(r.text.lstrip(")]}\',\n"))

for item in data['default']['timelineData']:
    print(item['formattedAxisTime'], item['value'])
Run Code Online (Sandbox Code Playgroud)

我们可以取消对 url 的引用,以便更好地了解发生了什么:

import urllib.parse

url = 'https://trends.google.com/trends/api/widgetdata/multiline?hl=en-GB&tz=-60&req=%7B%22time%22:%222018-05-29+2019-05-29%22,%22resolution%22:%22WEEK%22,%22locale%22:%22en-GB%22,%22comparisonItem%22:%5B%7B%22geo%22:%7B%22country%22:%22US%22%7D,%22complexKeywordsRestriction%22:%7B%22keyword%22:%5B%7B%22type%22:%22BROAD%22,%22value%22:%22sports+cars%22%7D%5D%7D%7D%5D,%22requestOptions%22:%7B%22property%22:%22%22,%22backend%22:%22IZG%22,%22category%22:0%7D%7D&token=APP6_UEAAAAAXO-yaYekqJ7Tf2nuoLBAigMSW7axoLTL&tz=-60'
print(urllib.parse.unquote(url))
Run Code Online (Sandbox Code Playgroud)

这产生:

'https://trends.google.com/trends/api/widgetdata/multiline?hl=en-GB&tz=-60&req={"time":"2018-05-29+2019-05-29","resolution":"WEEK","locale":"en-GB","comparisonItem":[{"geo":{"country":"US"},"complexKeywordsRestriction":{"keyword":[{"type":"BROAD","value":"sports+cars"}]}}],"requestOptions":{"property":"","backend":"IZG","category":0}}&token=APP6_UEAAAAAXO-yaYekqJ7Tf2nuoLBAigMSW7axoLTL&tz=-60'
Run Code Online (Sandbox Code Playgroud)

您需要探索其中的元素如何可转移。

例如,我查看了搜索词“香蕉”,结果如下:

未引用:

'https://trends.google.com/trends/api/explore?hl=en-GB&tz=-60&req={"comparisonItem":[{"keyword":"banana","geo":"US","time":"today+12-m"}],"category":0,"property":""}&tz=-60'
Run Code Online (Sandbox Code Playgroud)

引:

'https://trends.google.com/trends/api/explore?hl=en-GB&tz=-60&req=%7B%22comparisonItem%22:%5B%7B%22keyword%22:%22banana%22,%22geo%22:%22US%22,%22time%22:%22today+12-m%22%7D%5D,%22category%22:0,%22property%22:%22%22%7D&tz=-60'
Run Code Online (Sandbox Code Playgroud)