2016-09-22 113 views
0

我有低於設定的蟒蛇3.5代碼:如何使用python請求在google趨勢中下載csv文件?

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

令牌動態生成,從而沒有的js –

回答

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) 
+0

是啊,你不會那樣做,將工作,(忘了提),但我試圖把它作爲一般並且目標網址結尾處的令牌會有所不同。 –

+0

你可以連接基地的''URL與生成的部分...你能處理生成的部分? – Andersson

+0

我真的不知道,如何處理生成的url部分。 –