【问题标题】:How to download csv file in google trends using python requests?如何使用 python 请求在谷歌趋势中下载 csv 文件?
【发布时间】:2016-09-22 05:26:35
【问题描述】:

我有以下一组 python 3.5 代码:

from urllib.parse import unquote
import requests 

company_code = '%2Fm%2F07gyp7'
company_code_unquoted = unquote(company_code)
search_params = {'gprop' : 'news' , 'q' : company_code_unquoted}

root_url = 'https://www.google.com/trends/explore'
request_link = requests.get(root_url , params = search_params)
company_spec_url = request_link.url

这只会转到麦当劳的谷歌趋势网页。我想下载这个 xpath 中趋势的 csv 文件:

CSV_file_xpath = "/html/body/div[2]/div[2]/md-content/div/div/div[1]/trends-widget/ng-include/widget/div/div/div[1]/widget-actions/div/button[3]/div"

有没有办法不使用硒?

【问题讨论】:

  • token 是动态生成的,所以没有 Js 就不会这样做

标签: python-3.x csv selenium python-requests urllib


【解决方案1】:

试试下面的代码:

target_url = "https://www.google.com/trends/api/widgetdata/relatedsearches/csv?req={"restriction":{"geo":{},"time":"2011-09-22 2016-09-22","complexKeywordsRestriction":{"keyword":[{"type":"ENTITY","value":"/m/07gyp7"}]}},"keywordType":"QUERY","metric":["TOP","RISING"],"trendinessSettings":{"compareTime":"2006-09-20 2011-09-21"},"requestOptions":{"property":"","backend":"IZG","category":0},"language":"en"}&token=APP6_UEAAAAAV-TCqKauAaqofeqB8mprsxrUMYubV1Dd&tz=-180"
with open('C:/path/to/file/to/save/report.csv', 'wb') as my_csv:
    my_csv.write(requests.get(url).content)

【讨论】:

  • 是的,这会起作用,(忘了提)但我试图将它作为通用的,并且目标网址末尾的令牌会有所不同。
  • 您可以将基础URL 与生成的部分连接...您可以处理生成的部分吗?
  • 我还真不知道,怎么处理生成的部分url。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-17
  • 1970-01-01
  • 2014-01-08
相关资源
最近更新 更多