我正在使用Google Search Console API来获取每日数据,但是这太慢了,因为我也试图通过一些复杂的API请求来克服采样问题。
现在我正在查看批处理请求以执行相同的操作,但是文档非常少,没有任何示例。
我现在要做什么 我碰到了这个:https://developers.google.com/gmail/api/guides/batch 而且我可以看到批处理请求的格式,但是尽管我付出了所有努力,但我还是无法在Postman中提出正确的请求。
我也遇到了这个问题:Batch Request in python to Google Search Console API
而且仍然不确定在身份验证后我应该如何使用service
对象来实际生成批处理请求。
代码(我很喜欢):
from googleapiclient.http import BatchHttpRequest
class ExtractBulk:
def __init__(self):
self.data = []
def callback(self,request_id,response,exception):
if exception is not None:
print(exception)
pass
else:
print(request_id)
self.data.append(response)
def batchReq(self,req1,req2):
batch = BatchHttpRequest()
batch.add(req1,self.callback,request_id='001')
batch.add(req2,request_id='002')
batch.execute()
print(self.data)
def main():
service = authorize_creds() // just a function to do oauth2 authentication.
request_body_1 = {
"startDate": "2015-01-01","endDate": "2016-01-01","dimensions": ["country","device"]
}
request_body_2 = {
"startDate": "2016-01-01","endDate": "2017-01-01","device"]
}
s1 = service.searchanalytics().query(siteUrl=property_uri,body=request_body_1)
s2 = service.searchanalytics().query(siteUrl=property_uri,body=request_body_2)
batchReq(s1,s2)
This code is not really complete. For example: Where should I add `https://www.googleapis.com/batch/webmasters/v3` while using googleapi python library.