Skip to content

Commit c890377

Browse files
committed
Refactor scraping project
1 parent 6dbc05a commit c890377

File tree

1 file changed

+53
-42
lines changed

1 file changed

+53
-42
lines changed

web_scraping/scraping_project.py

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,61 @@
55
from random import choice
66
from csv import writer
77

8-
all_quotes = []
9-
base_url = "http://quotes.toscrape.com/"
10-
url = "/page/1"
8+
BASE_URL = "http://quotes.toscrape.com/"
119

12-
while url:
13-
res = requests.get(f"{base_url}{url}")
14-
print(f"Now scraping {base_url}{url}")
15-
soup = BeautifulSoup(res.text, "html.parser")
16-
quotes = soup.find_all(class_="quote")
17-
18-
for quote in quotes:
19-
all_quotes.append({
20-
"text": quote.find(class_="text").get_text(),
21-
"author": quote.find(class_="author").get_text(),
22-
"bio-link": quote.find("a")["href"]
23-
})
24-
next_btn = soup.find(class_="next")
25-
url = next_btn.find("a")["href"] if next_btn else None
26-
# sleep(3) # wait between scraping pages
27-
28-
quote = choice(all_quotes)
10+
def scrape_quotes():
11+
all_quotes = []
12+
url = "/page/1"
13+
while url:
14+
res = requests.get(f"{BASE_URL}{url}")
15+
print(f"Now scraping {BASE_URL}{url}")
16+
soup = BeautifulSoup(res.text, "html.parser")
17+
quotes = soup.find_all(class_="quote")
2918

30-
guess = ""
31-
remaining_guesses = 4
19+
for quote in quotes:
20+
all_quotes.append({
21+
"text": quote.find(class_="text").get_text(),
22+
"author": quote.find(class_="author").get_text(),
23+
"bio-link": quote.find("a")["href"]
24+
})
25+
next_btn = soup.find(class_="next")
26+
url = next_btn.find("a")["href"] if next_btn else None
27+
# sleep(3) # wait between scraping pages
28+
return all_quotes
3229

33-
print("Here's the quote: ")
34-
print(quote["text"])
30+
def start_game(quotes):
31+
quote = choice(quotes)
32+
guess = ""
33+
remaining_guesses = 4
34+
print("Here's the quote: ")
35+
print(quote["text"])
36+
while guess.lower() != quote["author"].lower() and remaining_guesses > 0:
37+
guess = input(f"Who said this quote? Guesses remaining: {remaining_guesses}\n")
38+
if guess.lower() == quote["author"].lower():
39+
print("YOU GOT IT RIGHT!")
40+
break
41+
remaining_guesses -= 1
42+
if remaining_guesses == 3:
43+
res = requests.get(f"{BASE_URL}{quote['bio-link']}")
44+
soup = BeautifulSoup(res.text, "html.parser")
45+
birth_date = soup.find(class_="author-born-date").get_text()
46+
birth_place = soup.find(class_="author-born-location").get_text()
47+
print(f"Here's a hint: The author was born on {birth_date} {birth_place}")
48+
elif remaining_guesses == 2:
49+
print(f"Here's a hint: The author's first name starts with: {quote['author'][0]}")
50+
elif remaining_guesses == 1:
51+
last_initial = quote['author'].split(" ")[1][0]
52+
print(f"Here's a hint: The author's last name starts with: {last_initial}")
53+
else:
54+
print(f"Sorry, you ran out of guesses. The answer was {quote['author']}")
3555

36-
while guess.lower() != quote["author"].lower() and remaining_guesses > 0:
37-
guess = input(f"Who said this quote? Guesses remaining: {remaining_guesses}\n")
38-
if guess.lower() == quote["author"].lower():
39-
print("YOU GOT IT RIGHT!")
40-
break
41-
remaining_guesses -= 1
42-
if remaining_guesses == 3:
43-
res = requests.get(f"{base_url}{quote['bio-link']}")
44-
soup = BeautifulSoup(res.text, "html.parser")
45-
birth_date = soup.find(class_="author-born-date").get_text()
46-
birth_place = soup.find(class_="author-born-location").get_text()
47-
print(f"Here's a hint: The author was born on {birth_date} {birth_place}")
48-
elif remaining_guesses == 2:
49-
print(f"Here's a hint: The author's first name starts with: {quote['author'][0]}")
50-
elif remaining_guesses == 1:
51-
last_initial = quote['author'].split(" ")[1][0]
52-
print(f"Here's a hint: The author's last name starts with: {last_initial}")
56+
again = ""
57+
while again.lower() not in ("y", "yes", "n", "no"):
58+
again = input("Would you like to lay again (y/n)?")
59+
if again.lower() in ("y", "yes"):
60+
return start_game(quotes)
5361
else:
54-
print(f"Sorry, you ran out of guesses. The answer was {quote['author']}")
62+
print("OK, GOODBYE")
63+
64+
quotes = scrape_quotes()
65+
start_game(quotes)

0 commit comments

Comments
 (0)