【问题标题】:Batch Request in python to Google Search Console APIpython中的批量请求到Google Search Console API
【发布时间】:2018-11-17 00:19:21
【问题描述】:

在过去的两天里,我一直在尝试使用 Python 向 Google Search Console API 发送批处理请求,但我不确定文档以及如何继续。

我不确定要遵循哪些文件。

https://developers.google.com/api-client-library/python/guide/batch

https://developers.google.com/webmaster-tools/v3/how-tos/batch

第一个文件说要使用这种格式:

from apiclient.http import BatchHttpRequest

def insert_animal(request_id, response, exception):
  if exception is not None:
    # Do something with the exception
    pass
  else:
    # Do something with the response
    pass

service = build('farm', 'v2')

batch = service.new_batch_http_request(callback=insert_animal)

batch.add(service.animals().insert(name="sheep"))
batch.add(service.animals().insert(name="pig"))
batch.add(service.animals().insert(name="llama"))
batch.execute(http=http)

但第二个帖子说要这样做:

POST /batch HTTP/1.1
Authorization: Bearer your_auth_token
Host: www.googleapis.com
Content-Type: multipart/mixed; boundary=batch_foobarbaz
Content-Length: total_content_length

--batch_foobarbaz
Content-Type: application/http
Content-ID: <item1:12930812@barnyard.example.com>

GET /farm/v1/animals/pony

--batch_foobarbaz
Content-Type: application/http
Content-ID: <item2:12930812@barnyard.example.com>

PUT /farm/v1/animals/sheep
Content-Type: application/json
Content-Length: part_content_length
If-Match: "etag/sheep"

{
  "animalName": "sheep",
  "animalAge": "5"
  "peltColor": "green",
}

--batch_foobarbaz
Content-Type: application/http
Content-ID: <item3:12930812@barnyard.example.com>

GET /farm/v1/animals
If-None-Match: "etag/animals"

--batch_foobarbaz--

但我不确定最后一个要我处理这段代码吗?

另外,当尝试在 Request 包中发出 Post 请求时,我在哪里可以输入 body 参数?

谢谢。

【问题讨论】:

标签: python google-api


【解决方案1】:

这是一种发出 GET 请求的技术,大概 POST 请求会非常相似。在此示例中,我们向搜索分析 API 发出批量请求。

#-------------------------------------#
# Define property and request payloads
#-------------------------------------#

web_property = 'https://example.com/'

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",
  "dimensions": ["country", "device"]
}

#--------------------------------#
# Make container to store results
#--------------------------------#

class DataContainer:
    def __init__(self):
        self.data = []

    def callback(self, request_id, response, exception):
        if exception is not None:
            # do something ...
            pass
        else:
            print(request_id)
            self.data.append(response)

dc = DataContainer()

#--------------------------------#
# Build and send batch requests
#--------------------------------#

batch = service.new_batch_http_request(callback=dc.callback)

batch.add(service.query(siteUrl=web_property,
                        body=request_body_1))
batch.add(service.query(siteUrl=web_property,
                        body=request_body_2))

batch.execute()
print(dc.data)

【讨论】:

    猜你喜欢
    • 2022-08-08
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多