Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from bs4 import BeautifulSoup
- import requests
- from random import choice
- # http://quotes.toscrape.com/
- quotes_db = [] # list with all of the quotes from website
- url = "http://quotes.toscrape.com"
- n = "http://quotes.toscrape.com"
- pages_list = [] # list with url's to the pages
- def get_q_from_page(link): # func that scrapes quotes, authors, url's to bio from the page
- response = requests.get(link, timeout=9)
- soup = BeautifulSoup(response.text, "html.parser")
- data = soup.find_all("div", class_="quote")
- for q in data:
- quote = q.find(class_="text").text
- author = q.find(class_="author").text
- a_tag = q.find("a")
- bio_url =str(a_tag).split('"')[1]
- bio = url + bio_url
- quotes_db.append([quote, author, bio])
- def get_all_pages(link): # func that scrapes url's from all the pages
- while True:
- pages_list.append(link)
- response = requests.get(link, timeout=9)
- soup = BeautifulSoup(response.text, "html.parser")
- n_page = soup.find("li", class_="next")
- if not n_page:
- break
- n_page = n_page.a
- n_page_url_part = str(n_page).split('"')[1]
- n_page_url = n + n_page_url_part
- link = n_page_url
- get_all_pages(url)
- for i in pages_list:
- get_q_from_page(i)
- game_is_playing = True
- attempt = 1
- while game_is_playing:
- info = choice(quotes_db)
- q = info[0]
- a = info[1]
- url_to_bio = info[2]
- response = requests.get(url_to_bio, timeout=9)
- soup = BeautifulSoup(response.text, "html.parser")
- author_title = soup.find("h3",class_="author-title").text
- author_name_lastname = author_title.split(" ")
- author_born_date = soup.find(class_="author-born-date").text
- author_born_location = soup.find(class_="author-born-location").text
- author_description = soup.find(class_="author-description").text
- author_description = author_description.replace(author_name_lastname[0], "XXXXX")
- author_description = author_description.replace(author_name_lastname[-1], "XXXXX")
- print("Hi! There is a quote for you:")
- print(q)
- answer = input("Who said this?: ").title()
- while attempt <= 4:
- if answer == a:
- print(f"Congratulations! You guessed in {attempt} try!")
- again = input("Do you want to play again? ")
- if not again.startswith("y"):
- print("Good bye!")
- game_is_playing = False
- break
- else:
- attempt = 1
- break
- else:
- attempt += 1
- print("your answer is wrong.")
- if attempt == 2:
- print(f"Here is a hint for You: Author of the quote was born in {author_born_date}, {author_born_location}")
- answer = input("Try to guess again: ").title()
- elif attempt == 3:
- print(f"Here is another hint: Authors name and lastname starts with: {author_title[0]}, {author_name_lastname[1][0]}")
- answer = input("Try one more time: ").title()
- elif attempt == 4:
- print("OK. Last hint for You:")
- print()
- print(author_description)
- print()
- answer = input("Last chance:").title()
- else:
- print(f"You loose. The answer is: {author_title}")
- game_is_playing = False
Advertisement
Add Comment
Please, Sign In to add comment