如何使用Python Requests模块登录Google?
发布时间:2020-12-20 13:35:35 所属栏目:Python 来源:网络整理
导读:我想使用 Python Requests模块与Google Trends进行互动. Google趋势需要身份验证才能访问Google趋势数据.有没有办法以编程方式登录请求?由于我没有使用Google API,因此OAuth 2.0似乎不适合我. 解决方法 我认为你实际上通过抓取它用来构建图形的原始JSON来获
我想使用
Python Requests模块与Google Trends进行互动. Google趋势需要身份验证才能访问Google趋势数据.有没有办法以编程方式登录请求?由于我没有使用Google API,因此OAuth 2.0似乎不适合我.
解决方法
我认为你实际上通过抓取它用来构建图形的原始JSON来获得更有趣的数据.它包括CSV下载未附带的相关标题.在达到配额之前,这适用于几个查询(5?).
import re import requests _GOOGLE_TRENDS_URL = 'http://www.google.com/trends/trendsReport?hl=en-US&content=1&q=%s&hl=en-US&content=1' term = 'foo' response = requests.get(_GOOGLE_TRENDS_URL % term) if response.status_code == requests.codes.ok: data_line = [l for l in response.content.splitlines() if 'var chartData' in l][0] chart_data = re.sub(r'.*var chartData = (.*?);.*',r'1',data_line) # Fix for date representation chart_data = re.sub(r'new Date((d+),(d+),(d+))',r'"1-2-3"',chart_data) data = json.loads(chart_data) #data = { # ...,# "rows": [ # [ # { # "f": "January 2004",# "v": "2004-0-16" # },# Date # null,# annotation # null,# annotation text # 91,# count # null,# annotation (2?) # null,# annotationText (2?) # true # certainty # ],#... # ] #} for row in data['rows']: if '2013' in row[0]['v']: print '%s: %d' % (row[0]['f'],row[3]) else: print response.status_code print response.text 产量: January 2013: 21 February 2013: 21 March 2013: 21 April 2013: 20 May 2013: 20 June 2013: 20 July 2013: 20 August 2013: 21 September 2013: 19 October 2013: 20 November 2013: 21 December 2013 (partial data): 22 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |