Next Google Custom Search

I have the following code and I don’t know how to print links on 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() 
+6
source share
2 answers

The response object contains the dictionary "nextPage". You can use this to determine the starting index of the next query. For instance:

 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() 
+6
source

My suggestion is to add the following parameter. In current software you have q, cx and num. You can try adding start = 10, and then execute the code.

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

The URL of the first result does not have an initial parameter. The second page contains a URL that contains the parameter start = 10. The third page has a URL that contains start = 20 ...

Good luck.

+4
source

Source: https://habr.com/ru/post/920858/


All Articles