Scrape quotes using Python.

Free Python Code - Aug 21 '23 - - Dev Community

Hi 🙂🖐

In this post i aill share with you how to Scrape quotes using Python.

I will to Scrape quotes from
https://quotes.toscrape.com/page/1/

step 1

Select html elements

Get quote class name

Image description

Get author name

Image description

from bs4 import BeautifulSoup
import requests

url = 'https://quotes.toscrape.com/page'

def scrape(url, page_num):
    url = f'{url}/{page_num}'
    res = requests.get(url)
    html = BeautifulSoup(res.content, 'html.parser')
    quotes = []
    authors = []

    for quote in html.find_all('span', class_ = 'text'):
        quotes.append(quote.text)

    for author in html.find_all('small', class_ = 'author'):
        authors.append(author.text)


    for i, quote in enumerate(quotes):
        f = open('quotes.txt', 'a+', errors = 'ignore')
        f.write(f'{quote} ## {authors[i]}\n')
        f.close()    


for i in range(20):
    scrape(url, i)

Enter fullscreen mode Exit fullscreen mode

Now we're done 🤗

Don't forget to like and follow 🙂

Support me on PayPal 🤗
https://www.paypal.com/paypalme/amr396

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .