- Notifications
You must be signed in to change notification settings - Fork 46.7k
/
Copy pathcrawl_google_results.py
24 lines (21 loc) · 811 Bytes
/
crawl_google_results.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
importsys
importwebbrowser
importrequests
frombs4importBeautifulSoup
fromfake_useragentimportUserAgent
if__name__=="__main__":
print("Googling.....")
url="https://www.google.com/search?q="+" ".join(sys.argv[1:])
res=requests.get(url, headers={"UserAgent": UserAgent().random}, timeout=10)
# res.raise_for_status()
withopen("project1a.html", "wb") asout_file: # only for knowing the class
fordatainres.iter_content(10000):
out_file.write(data)
soup=BeautifulSoup(res.text, "html.parser")
links=list(soup.select(".eZt8xd"))[:5]
print(len(links))
forlinkinlinks:
iflink.text=="Maps":
webbrowser.open(link.get("href"))
else:
webbrowser.open(f"https://google.com{link.get('href')}")