我有以下的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"
有没有办法,不使用硒?
答案 0 :(得分:0)
请尝试以下代码:
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)