forked from adds-rag/TouchlessBrowse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_text_from_website.py
35 lines (24 loc) · 941 Bytes
/
get_text_from_website.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
import requests
from bs4 import BeautifulSoup
''' DONE
MAIN FUNCTION
Function to get all text from a website given a URL and return it as a string.
Params:
url - the url of the website to get text from
Returns:
A string of all text from the website
'''
def get_text_from_website(url):
try:
# Send a GET request to the URL
response = requests.get(url)
response.raise_for_status() # Raise an HTTPError for bad responses
# Parse the HTML content using BeautifulSoup
soup = BeautifulSoup(response.content, 'html.parser')
# Extract all text from the HTML
text = soup.get_text(separator='\n', strip=True)
return text
except requests.exceptions.RequestException as e:
print(f"Error fetching content from {url}: {e}")
return None
print(get_text_from_website("https://en.wikipedia.org/wiki/Introspection"))