LoginSignup
8
7

More than 5 years have passed since last update.

google custom searchを使う

Posted at

制約

  • 1日100リクエスト
  • 1ページ10件
  • 10ページまでしか取得できない
  • つまり100件しかURLとれない

マニュアルは
https://developers.google.com/custom-search/v1/using_rest

api keyの取得

API Consoleより取得できる

サンプルコード

結果のjsonをファイルとして出力する

googlesearch.py
#! /usr/bin/python
# -*- coding:utf-8 -*-

import urllib
import json
QUERY = ''
API = ''
NUM = 10

url = 'https://www.googleapis.com/customsearch/v1?'
params = {
    'key': API_KEY,
    'q':QUERY,
    'cx':'013036536707430787589:_pqjad5hr1a',
    'alt':'json',
    'lr' :'lang_en',
}
start = 1
f = open('googleresult.json','w')

for i in range(0,NUM):
    params['start'] = start
    req_url = url + urllib.urlencode(params)
    res = urllib.urlopen(req_url)
    dump = json.loads(res.read())
    f.write(json.dumps(dump) + "\n")
    if not dump['queries'].has_key('nextPage'):
        break
    start = int(dump['queries']['nextPage'][0]['startIndex'])

f.close()

8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7