MikeWP

Untitled

Apr 18th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.96 KB | None | 0 0
  1. from bs4 import BeautifulSoup
  2. import requests
  3. from random import choice
  4. # http://quotes.toscrape.com/
  5.  
  6. quotes_db = [] # list with all of the quotes from website
  7. url = "http://quotes.toscrape.com"
  8. n = "http://quotes.toscrape.com"
  9. pages_list = [] # list with url's to the pages
  10.  
  11.  
  12. def get_q_from_page(link): # func that scrapes quotes, authors, url's to bio from the page
  13.     response = requests.get(link, timeout=9)
  14.     soup = BeautifulSoup(response.text, "html.parser")
  15.     data = soup.find_all("div", class_="quote")
  16.  
  17.     for q in data:
  18.         quote = q.find(class_="text").text
  19.         author = q.find(class_="author").text
  20.         a_tag = q.find("a")
  21.         bio_url =str(a_tag).split('"')[1]
  22.         bio = url + bio_url
  23.         quotes_db.append([quote, author, bio])
  24.  
  25.  
  26. def get_all_pages(link): # func that scrapes url's from all the pages
  27.     while True:
  28.         pages_list.append(link)
  29.         response = requests.get(link, timeout=9)
  30.         soup = BeautifulSoup(response.text, "html.parser")
  31.         n_page = soup.find("li", class_="next")
  32.         if not n_page:
  33.             break
  34.         n_page = n_page.a
  35.         n_page_url_part = str(n_page).split('"')[1]
  36.         n_page_url = n + n_page_url_part
  37.         link = n_page_url
  38.  
  39.  
  40. get_all_pages(url)
  41. for i in pages_list:
  42.     get_q_from_page(i)
  43.  
  44. game_is_playing = True
  45. attempt = 1
  46. while game_is_playing:
  47.     info = choice(quotes_db)
  48.     q = info[0]
  49.     a = info[1]
  50.     url_to_bio = info[2]
  51.     response = requests.get(url_to_bio, timeout=9)
  52.     soup = BeautifulSoup(response.text, "html.parser")
  53.     author_title = soup.find("h3",class_="author-title").text
  54.     author_name_lastname = author_title.split(" ")
  55.     author_born_date = soup.find(class_="author-born-date").text
  56.     author_born_location = soup.find(class_="author-born-location").text
  57.     author_description = soup.find(class_="author-description").text
  58.     author_description = author_description.replace(author_name_lastname[0], "XXXXX")
  59.     author_description = author_description.replace(author_name_lastname[-1], "XXXXX")
  60.     print("Hi! There is a quote for you:")
  61.     print(q)
  62.     answer = input("Who said this?: ").title()
  63.     while attempt <= 4:
  64.         if answer == a:
  65.             print(f"Congratulations! You guessed in {attempt} try!")
  66.             again = input("Do you want to play again? ")
  67.             if not again.startswith("y"):
  68.                 print("Good bye!")
  69.                 game_is_playing = False
  70.                 break
  71.             else:
  72.                 attempt = 1
  73.                 break
  74.         else:
  75.             attempt += 1
  76.             print("your answer is wrong.")
  77.             if attempt == 2:
  78.                 print(f"Here is a hint for You: Author of the quote was born in {author_born_date}, {author_born_location}")
  79.                 answer = input("Try to guess again: ").title()
  80.             elif attempt == 3:
  81.                 print(f"Here is another hint: Authors name and lastname starts with: {author_title[0]}, {author_name_lastname[1][0]}")
  82.                 answer = input("Try one more time: ").title()
  83.             elif attempt == 4:
  84.                 print("OK. Last hint for You:")
  85.                 print()
  86.                 print(author_description)
  87.                 print()
  88.                 answer = input("Last chance:").title()
  89.  
  90.     else:
  91.         print(f"You loose. The answer is: {author_title}")
  92.         game_is_playing = False
Advertisement
Add Comment
Please, Sign In to add comment