-
Notifications
You must be signed in to change notification settings - Fork 0
/
quotes_scraper.py
79 lines (62 loc) · 2.58 KB
/
quotes_scraper.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import requests
from bs4 import BeautifulSoup
from time import sleep
from random import choice
BASE_URL = "https://quotes.toscrape.com/"
def scrape_quotes():
all_quotes = list()
url = "/page/1/"
while url:
resp = requests.get(f"{BASE_URL}{url}")
print(f"Now Scraping {BASE_URL}{url} ......")
soup = BeautifulSoup(resp.text, "html.parser")
quotes = soup.find_all(class_="quote")
for quote in quotes:
text = quote.find(class_="text").get_text()
author = quote.find(class_="author").get_text()
bio = quote.find("a")["href"]
all_quotes.append({
"text": text,
"author": author,
"bio": bio
})
next_btn = soup.find(class_="next")
url = next_btn.find("a")["href"] if next_btn else None
sleep(2)
return all_quotes
def start_game(quotes):
quote = choice(quotes)
remaining_guesses = 4
guess = ""
print("Here's a quote: ")
print(quote["text"])
print(quote["author"])
while guess.lower() != quote["author"].lower() and remaining_guesses > 0:
guess = input(f"Who said this quote? \n Guesses remaining: {remaining_guesses} \n")
remaining_guesses -= 1
if guess.lower() == quote["author"].lower():
print("YOU GOT IT RIGHT BRUHHH !!!")
elif remaining_guesses == 3:
res = requests.get(f"{BASE_URL}{quote['bio']}")
soup = BeautifulSoup(res.text, "html.parser")
birth_date = soup.find(class_="author-born-date").get_text()
birth_location = soup.find(class_="author-born-location").get_text()
print(f"Here's a hint for you : The author was born on {birth_date} {birth_location}")
elif remaining_guesses == 2:
first_initial = quote['author'][0]
print(f"Here's a hint : The author's first name starts with: {first_initial}")
elif remaining_guesses == 1:
last_inital = quote['author'].split(" ")[1][0]
print(f"Here's a hint : The author's last name starts with: {last_inital}")
else:
print(f"Sorry you ran out of guesses.\n The answer was {quote['author']}")
again = " "
while again.lower() not in ('y', 'yes', 'n', 'no'):
again = input("Would you like to play again (yes/no)?")
if again.lower() in ('y', 'yes'):
print("Ok you play again!")
return start_game(quotes)
else:
print("Ok, goodbye!")
quotes = scrape_quotes()
start_game(quotes)