i have a problem with a school project, that says i should make a scraper program with beautifulSoup that will be a weeding surprise and will print out random 5 quotes out of the page they gave me ....well I came to a logic how to scrape the data out of the site and it scrapes the quotes but it doesn't return 5 of them but all of them.....i tried with counter but no luck could you help me out please?
import urllib
from BeautifulSoup import BeautifulSoup
topic_url = 'http://quotes.yourdictionary.com/theme/marriage/'
topic_html = urllib.urlopen(topic_url).read()
topic_soup = BeautifulSoup(topic_html)
quotes = topic_soup.findAll('p', attrs={'class': 'quoteContent'})
for quote in quotes:
print quote.text + ("\n")
You already have all the quotes stored in the quotes
variable. You can select 5 random ones from that collection with the random
module:
import random
five_quotes = random.sample(quotes, 5)
for quote in five_quotes:
print(quote.text + "\n")