-
Notifications
You must be signed in to change notification settings - Fork 1
/
bing-search.py
54 lines (45 loc) · 1.97 KB
/
bing-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
# bing-search.py
# (c) 2020 CincoNoveSeis Jornalismo Ltda.
#
# This program is licensed under the GNU General Public License, version 3.
# See the LICENSE file for details.
from azure.cognitiveservices.search.websearch import WebSearchClient
from msrest.authentication import CognitiveServicesCredentials
from pprint import pprint
import time
import csv
import traceback
with open('gapi-key-oscar.txt', 'r') as f:
with open('bing_input.txt', 'r') as inp:
with open('gapi-endpoint.txt', 'r') as ff:
subscription_key = f.read().strip()
endpoint = ff.read().strip()
client = WebSearchClient(
endpoint=endpoint,
credentials=CognitiveServicesCredentials(subscription_key))
tse_ids = [x.strip() for x in inp.readlines()]
oup_dicts = []
for tse_id in tse_ids:
print(tse_id)
try:
time.sleep(0.4)
web_data = client.web.search(
query = '{}'.format(tse_id),
count = 10,
mkt = 'pt-BR',
responseFilter = 'Webpages')
if hasattr(web_data.web_pages, 'value'):
for page in web_data.web_pages.value:
oup_dicts.append({
'tse_id': tse_id,
'url': page.url,
'name': page.name,
'snippet': page.snippet
})
except Exception as e:
traceback.print_exc()
break
with open('bing_output.csv', 'w', newline='\n', encoding='utf-8') as oup:
dict_writer = csv.DictWriter(oup, oup_dicts[0].keys(), lineterminator='\n')
dict_writer.writeheader()
dict_writer.writerows(oup_dicts)