-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProject01.py
35 lines (29 loc) · 1.12 KB
/
Project01.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
def scrape_quotes():
# Send a GET request to the website
url = "http://quotes.toscrape.com"
response = requests.get(url)
if response.status_code == 200:
# Parse the HTML content of the page
soup = BeautifulSoup(response.text, 'html.parser')
# Extract information (quotes and authors) from the page
quotes = []
for quote in soup.find_all('div', class_='quote'):
text = quote.find('span', class_='text').text
author = quote.find('small', class_='author').text
quotes.append({'text': text, 'author': author})
return quotes
else:
print("Failed to retrieve the page. Status code:", response.status_code)
return None
if __name__ == "__main__":
# Run the web scraping function
scraped_quotes = scrape_quotes()
# Display the results
if scraped_quotes:
print("Scraped Quotes:")
for index, quote in enumerate(scraped_quotes, start=1):
print(f"{index}. {quote['text']} - {quote['author']}")
else:
print("Web scraping failed.")