Google Search Console API-批处理请求-Python

我正在使用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.

相关文章

np.linspace函数的基本语法如下:numpy.linspace(start,&nbs...
Python中的函数(二) 在上一篇文章中提到了Python中函数的定...
Python中的字符串 可能大多数人在学习C语言的时候,最先接触...
Python 面向对象编程(一) 虽然Python是解释性语言,但是它...
Python面向对象编程(二) 在前面一篇文章中谈到了类的基本定...
Python中的函数(一) 接触过C语言的朋友对函数这个词肯定非...