-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgooglesearch.py
More file actions
31 lines (26 loc) · 774 Bytes
/
googlesearch.py
File metadata and controls
31 lines (26 loc) · 774 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import urllib
import urllib.request
import urllib.parse
import json
def simple_search(query):
QUERY = query
API_KEY = 'AIzaSyBdhBTUc5W5Aco3YGPwOlS_rYM0LBKl_joo'
NUM = 1
url = 'https://www.googleapis.com/customsearch/v1?'
params = {
'key': API_KEY,
'q': QUERY,
'cx': '013036536707430787589:_pqjad5hr1a',
'alt': 'json',
'lr': 'lang_ja', }
start = 1
for i in range(0, NUM):
params['start'] = start
request_url = url + urllib.parse.urlencode(params)
try:
response = urllib.request.urlopen(request_url)
json_body = json.loads(response.read().decode('utf-8'))
items = json_body['items']
except:
print('Error')
return items