2012-07-19 26 views
6

私は次のコードを持っていますが、次のページのリンクを印刷する方法、次のページに移動する方法はわかりません。Googleカスタム検索次のページ

#!/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

レスポンスオブジェクトが「NEXTPAGE」辞書が含まれている= 20 ...

幸運を開始含まれているURLを持っています。これを使用して、次の要求の開始インデックスを決定することができます。ように:

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() 
4

私の提案は次のパラメータを追加することです。現在のソフトウェアにはq、cx、numがあります。 start = 10を追加してからコードを実行してみてください。

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

最初の結果ページURLには開始パラメータがありません。 2番目のページには、start = 10パラメータを含むURLがあります。 3ページ目は

関連する問題