Google custom search next page

 ̄綄美尐妖づ 提交于 2019-12-21 04:32:20

问题


I have the following code, and i don't know how to print the links of the next page, how to go to the next pages?

#!/usr/bin/python2.4
# -*- coding: utf-8 -*-


import pprint

from apiclient.discovery import build


def main():

    service = build("customsearch", "v1",
                 developerKey="")

    res = service.cse().list(
         q='lectures',
         cx='013036536707430787589:_pqjad5hr1a',
         num=10, #Valid values are integers between 1 and 10, inclusive.
    ).execute() 

    for value in res:
        #print value
        if 'items' in value:
            for results in res[value]:
                print results['formattedUrl'] 

if __name__ == '__main__':
  main()

回答1:


The response object contains a 'nextPage' dictionary. You can use this to determine the start index of the next request. Like so:

res = service.cse().list(
     q='lectures',
     cx='013036536707430787589:_pqjad5hr1a',
     num=10, #Valid values are integers between 1 and 10, inclusive.
).execute() 

next_response = service.cse().list(
     q='lectures',
     cx='013036536707430787589:_pqjad5hr1a',
     num=10,
     start=res['queries']['nextPage'][0]['startIndex'],
).execute() 



回答2:


My proposition is to add next parameter. In current software you have q, cx and num. You could try add start=10 and then execute the code.

res = service.cse().list(
    q='lectures',
    cx='013036536707430787589:_pqjad5hr1a',
    num=10,
    start=10,
).execute()

First result page URL doesn't have start parameter. Second page has URL which contains start=10 parameter. Third page has URL which contains start=20 ...

Good luck



来源:https://stackoverflow.com/questions/11554916/google-custom-search-next-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!