【问题标题】:google apiclient for searching images in pythongoogle apiclient 用于在 python 中搜索图像
【发布时间】:2016-04-16 22:35:52
【问题描述】:

我想获取对 Google apiclient.discovery 的查询产生的图像的 url。使用下面的代码,我可以获得前 10 张图像,但是当我在查询中添加字段“开始”以迭代接下来的 10 张图像时,我收到以下错误消息:

HttpError: https://www.googleapis.com....."无效值">

我的代码:

from apiclient.discovery import build

curr_idx = 0
service = build("customsearch", "v1",developerKey="***mykey***")
for query in range(3):
    res = service.cse().list(
        q='cat',
        cx='***myengine***',
        searchType='image',
        start=curr_idx,
        num=10,
    ).execute()

    for item in res['items']:
        print item['title']
        curr_idx = curr_idx + 1

有人知道为什么吗?

【问题讨论】:

    标签: python google-api-client


    【解决方案1】:

    我找到了解决方案。 curr_idx 必须是一个字符串,初始值应该是 1 而不是 0。所以这是最终代码

    from apiclient.discovery import build
    
    curr_idx = 1
    service = build("customsearch", "v1",developerKey="***mykey***")
    for query in range(3):
        res = service.cse().list(
            q='cat',
            cx='***myengine***',
            searchType='image',
            start=str(curr_idx),
            num=10,
        ).execute()
    
        for item in res['items']:
            print item['title']
            curr_idx = curr_idx + 1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-02
      • 1970-01-01
      • 1970-01-01
      • 2021-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多