-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyscri.py
More file actions
60 lines (41 loc) · 1.48 KB
/
Copy pathpyscri.py
File metadata and controls
60 lines (41 loc) · 1.48 KB
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
59
60
import sys
import json
import ast # abstract syntax tree
#-----Web scrape code --------
import requests
from bs4 import BeautifulSoup
input_data = ast.literal_eval(sys.argv[1])
output_data = []
#print("Received data from Node.js:", input_data)
for val in input_data:
query = val.replace(" ", "+")
#print("Query:", query)
page=requests.get(f"https://www.bing.com/search?q={query}").content
soup=BeautifulSoup(page, "html.parser")
#print(soup.prettify)
items = soup.select('div li h2 a')
# Strings to pass back to Node.js
try:
text1 = items[3].text.strip()
hyperlink1 = items[3]['href']
output_data.append({'text': text1, 'hyperlink': hyperlink1})
except:
pass
try:
text2 = items[4].text.strip()
hyperlink2 = items[4]['href']
output_data.append({'text': text2, 'hyperlink': hyperlink2})
except:
pass
try:
text3 = items[5].text.strip()
hyperlink1 = items[5]['href']
output_data.append({'text': text3, 'hyperlink': hyperlink3})
except:
pass
# -----------------------------
# Serialize the list of strings to JSON
serialized_output = json.dumps(output_data)
print(serialized_output)
sys.stdout.flush()
# Ensure the output is flushed immediatel