I've done simple web scraping and want to make sure all my steps are correct? Is it considered clean code? Is there a better way to use the multi-page scraping feature?
import requests
from bs4 import BeautifulSoup
import pandas as pd
def main():
data = []
for page_num in range(1,51):
url = f'https://books.toscrape.com/catalogue/page-{page_num}.html'
headers = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36'}
response = requests.get(url, headers = headers)
soup = BeautifulSoup(response.content, "lxml")
books = soup.find_all('article', class_ = 'product_pod')
for book in books:
name = book.find('img').attrs['alt']
price = book.find('p', class_ = 'price_color').text.strip()
link = 'https://books.toscrape.com/' + book.find('a').attrs['href']
stock = book.find('p', class_ = 'instock availability').text.strip()
data.append([name, price, link,stock])
df = pd.DataFrame(data, columns=['name', 'price', 'link', 'stock'])
df.to_csv('data.csv')
main()