-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgoogle_search.py
58 lines (49 loc) · 2 KB
/
google_search.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from google import google
import json
def test():
query_list = ['Crista Lopes','mondego','machine learning','software engineering','security',\
'student affairs','graduate courses','REST','computer games','information retrieval']
num_page = 2
for query in query_list:
search_results = google.search(query + " site:ics.uci.edu", num_page)
for doc in search_results[0:5]:
print(doc.link)
print()
# print(search_results[0:5])
def google_search_query(query):
num_page = 1
search_results = google.search(query + " site:ics.uci.edu", num_page)
return search_results[0:5]
def google_search_query_pages(query, num_pages):
# num_pages = 3
search_results_pages = google.search(query + " site:ics.uci.edu", num_pages)
return search_results_pages
def google_search_query_dict(query):
res = google_search_query_pages(query,20)
link_list = []
for i,doc in enumerate(res,1):
if ('.pdf' in doc.link) or ('.PDF' in doc.link) or ('.ppt' in doc.link) or ('.pptx' in doc.link):
continue
link_list.append((doc.link,i))
res_dict = dict(link_list)
return res_dict
def save_query_res_to_json(query_list):
query_dict = dict()
for query in query_list:
# res = google_search_query_pages(query,10)
# link_list = []
# for i,doc in enumerate(res,1):
# link_list.append((doc.link,i))
# res_dict = dict(link_list)
res_dict = google_search_query_dict(query)
query_dict[query] = res_dict
with open('google_results.json', 'w') as outfile:
json.dump(query_dict, outfile)
if __name__ == '__main__':
query_list = ['Crista Lopes','mondego','machine learning','software engineering','security',\
'student affairs','graduate courses','REST','computer games','information retrieval']
save_query_res_to_json(query_list)
# res = google_search_query_pages('REST',10)
# print(len(res))
# for doc in res:
# print(doc.link)