make a script that downloads books from csv file in python

Here's a sample script that downloads books from a CSV file using the requests and BeautifulSoup libraries:

main.py
import csv
import requests
from bs4 import BeautifulSoup

# Set the base URL and create a session
BASE_URL = 'http://books.toscrape.com/'
session = requests.Session()

# Open the CSV file and loop through each row
with open('books.csv', newline='') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        # Get the book URL from the CSV and append it to the base URL
        book_url = BASE_URL + row[0]

        # Send a GET request to the book URL
        response = session.get(book_url)

        # Parse the HTML using BeautifulSoup
        soup = BeautifulSoup(response.text, 'html.parser')

        # Find the book title and print it
        title = soup.find('h1').text.strip()
        print(title)

        # Find the book cover URL and download the image
        img_url = soup.find('img')['src']
        img_response = session.get(BASE_URL + img_url)
        with open(title + '.jpg', 'wb') as img_file:
            img_file.write(img_response.content)

        # Find the book description and print it
        description = soup.find('div', {'id': 'product_description'}).find_next('p').text.strip()
        print(description)

        # Find the book price and print it
        price = soup.find('p', {'class': 'price_color'}).text.strip()
        print(price)

        print('-----------------------')

1336 chars
42 lines

This script opens a CSV file containing book URLs and downloads each book's cover image and prints its title, description, and price. It uses a session to send HTTP requests, which persist cookies between requests for efficiency. The BeautifulSoup library is used to parse the book's HTML and extract the necessary information.

gistlibby LogSnag