-
Notifications
You must be signed in to change notification settings - Fork 0
/
webscraping.py
56 lines (42 loc) · 1.64 KB
/
webscraping.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
import requests
from bs4 import BeautifulSoup
def web_scraping(qs):
URL = 'https://www.google.com/search?q=' + qs
page = requests.get(URL)
soup = BeautifulSoup(page.content, 'html.parser')
links = soup.findAll("a")
all_links = []
for link in links:
link_href = link.get('href')
if "url?q=" in link_href and not "webcache" in link_href:
all_links.append((link.get('href').split("?q=")[1].split("&sa=U")[0]))
flag = False
for link in all_links:
if 'https://en.wikipedia.org/wiki/' in link:
wiki = link
flag = True
break
div0 = soup.find_all('div', class_="kvKEAb")
div1 = soup.find_all("div", class_="Ap5OSd")
div2 = soup.find_all("div", class_="nGphre")
div3 = soup.find_all("div", class_="BNeawe iBp4i AP7Wnd")
if len(div0) != 0:
answer = div0[0].text
elif len(div1) != 0:
answer = div1[0].text + "\n" + div1[0].find_next_sibling("div").text
elif len(div2) != 0:
answer = div2[0].find_next("span").text + "\n" + div2[0].find_next("div", class_="kCrYT").text
elif len(div3) != 0:
answer = div3[1].text
elif flag == True:
page2 = requests.get(wiki)
soup = BeautifulSoup(page2.text, 'html.parser')
title = soup.select("#firstHeading")[0].text
paragraphs = soup.select("p")
for para in paragraphs:
if bool(para.text.strip()):
answer = title + "\n" + para.text
break
else:
answer = "Sorry. I could not find the desired results"
return answer