-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc_t1.py
More file actions
29 lines (18 loc) · 767 Bytes
/
Copy pathc_t1.py
File metadata and controls
29 lines (18 loc) · 767 Bytes
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
import requests
from bs4 import BeautifulSoup
import re
def text(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
paragraphs = soup.find_all(['p']) # extract all paragraphs, headings, lists, tables, and divs from the article
text_data = []
for element in paragraphs:
text_data.append(element.get_text())
text_string = ' '.join(text_data) # join the list of text strings into a single string
if len(text_string)>1000:
text_string=text_string[0:1000]
else:
text_string=text_string
text_string=re.sub(r'[^\w\s]|\d|[\n]','',text_string)
# print the text string
return text_string