forked from shashnkvats/Chat-Wiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwiki_content.py
38 lines (29 loc) · 1018 Bytes
/
wiki_content.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
import requests
import wikipedia
from bs4 import BeautifulSoup
def get_wiki(search):
# set language to English (default is auto-detect)
lang = "en"
"""
fetching summary from wikipedia
"""
# set language to English (default is auto-detect)
summary = wikipedia.summary(search, sentences = 5)
"""
scrape wikipedia page of the requested query
"""
# create URL based on user input and language
url = f"https://{lang}.wikipedia.org/wiki/{search}"
# send GET request to URL and parse HTML content
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
# extract main content of page
content_div = soup.find(id="mw-content-text")
# extract all paragraphs of content
paras = content_div.find_all('p')
# concatenate paragraphs into full page content
full_page_content = ""
for para in paras:
full_page_content += para.text
# print the full page content
return full_page_content, summary