Question
I wrote this page scraper using python. I want to get all the prices of the tires and store them in a csv file. All
I wrote this page scraper using python. I want to get all the prices of the tires and store them in a csv file. All the prices are stored in a html structure like this. How do I grab each price and store it to the csv file along with the tire name in 2 columns?
import requests import csv from datetime import datetime from bs4 import BeautifulSoup
# for tire and review page
# download the page myurl = requests.get("https://www.goodyearautoservice.com/en-US/tires/all-season")
# create BeautifulSoup object soup = BeautifulSoup(myurl.text, 'html.parser')
# get all the tire info we need, specify where the info is located h3 = soup.find_all('h3', class_ = "baseball-card__title")
for tireblock in h3: # find the children in tire block children = tireblock.findChildren() first = [] for child in children: # grab the text in the b tags print(child.get_text())
price = soup.find_all('span', class_ = "baseball-card__price") for listprice in price: orig = price[0].text.strip() print(orig)
# create csv file name, and use 'w' since we are only writing to file with open('tireinfo.csv', 'w') as csv_file: # iterate through our arrays for f in zip(first): # concatenate both lines to match them p csv_file.write(str(f)) # for csv file formatting, start each on a new line csv_file.write(" ") print(f)
?Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started