As in the title, I’m trying to write a Ebay web-scrape program, yet when I try to find the price, it creates a list error, yet it works for getting the product name.
The url is: https://www.ebay.com.au/sch/i.html?_from=R40&_nkw=switch&_sacat=0&_pgn=1
import bs4
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
**Open Collection**
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close
grabs each products
containers = page_soup.findAll("div", {"class" : "s-item__wrapper clearfix"})
filename = "EbayWebscraping.csv"
f = open(filename, "w")
headers = "product_name, quality"
for container in containers:
title_container = container.findAll('h3', {'class' : 's-item__title'} )
product_name = title_container[0].text
#Where the problem is#
price_container = container.findAll('span', {'class' : 's-item__price'})
price = price_container[0].text
print('Product: ' + product_name)
print('Price: ' + price)
2
Answers
if you see
containers
in which at index 0 there is no product or price info so you can start from index 1 and also you can usetry-except
instead of thatOutput:
You can also check if the selector is present before doing further processing:
You also don’t need to access
[0]
index.text
would work perfectly. Additionally, there’s no need to usefindAll
since you already extracting data fromcontainers
and its selector that contains data about title, price inside. Think of the container as matryoshka doll if it makes more sense.You just have to call text and price selectors e.g:
Code that paginates through all pages and example in online IDE.
Example output
As an alternative, you can use Ebay Organic Results API from SerpApi. It’s a paid API with a free plan that handles blocks and parsing on their backend.
Example code that paginates through all pages:
Output: