I’m a very new coder, and I’m using Collab and Python. I’m trying to scrape eBay for information on products, primarily sneakers. I want to scrape the fields on the listings and need the code to return the size, condition, model, year, authenticity guarantee (from eBay), and more. I’ve tried checking the APIs that eBay calls when the page is refreshed, but (I think) they encrypt the information. I’m on Google Collab right now running Python. enter image description here
CODE
import requests
from bs4 import BeautifulSoup
def scrape_ebay(search_query, num_pages=5):
for page in range(1, num_pages + 1):
url = f"https://www.ebay.com/sch/i.html?_nkw={search_query}&_pgn={page}"
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
listings = soup.find_all('li', attrs={'class': 's-item'})
for listing in listings:
img_tag = listing.find('img')
title = img_tag.get('alt') if img_tag else "No title"
image_url = img_tag.get('src') if img_tag else "No image URL"
price = listing.find('span', attrs={'class': 's-item__price'}).text if listing.find('span', attrs={'class': 's-item__price'}) else "No price"
# Corrected code to extract shoe size
size_element = listing.find('span', class_='ux-textspans')
size = size_element.text.strip() if size_element else "No size info"
print(f"Title: {title}, Price: {price}, Image URL: {image_url}, Size: {size}")
# Replace 'sneakers' with your search query
scrape_ebay('sneakers', num_pages=10)
`
RESPONSE
Title: Shop on eBay, Price: $20.00, Image URL: https://ir.ebaystatic.com/rs/v/fxxj3ttftm5ltcqnto1o4baovyl.png, Size: No size info
Title: NEW Nike Dunk Low Panda Black White (Size 7 - 13 Mens) or Womens or Grade School, Price: $134.99, Image URL: https://i.ebayimg.com/thumbs/images/g/iIgAAOSwKCJmMnTV/s-l500.jpg, Size: No size info
Title: Nike Lunarglide 6 Running Shoes Black Teal Pink Sneakers Womens Size 10.5, Price: $16.65, Image URL: https://i.ebayimg.com/thumbs/images/g/8rEAAOSwR8dlhkVr/s-l500.jpg, Size: No size info
Title: adidas men Samoa Shoes, Price: $48.00, Image URL: https://i.ebayimg.com/thumbs/images/g/ookAAOSwYRJmRr1A/s-l140.jpg, Size: No size info
Title: adidas men Runfalcon 2.0 TR Running Shoes, Price: $39.00, Image URL: https://i.ebayimg.com/thumbs/images/g/jdMAAOSwjjJmR4VM/s-l140.jpg, Size: No size info
Title: adidas men Gazelle ADV Shoes, Price: $60.00, Image URL: https://i.ebayimg.com/thumbs/images/g/upQAAOSwOZBmR6kc/s-l140.jpg, Size: No size info
_____
_
That’s what i have as of now. it’s correctly scraping the Image URL, price, and title. I can’t seem to get the size working. For context, here heres how the page looks. the box on the bottom is the info i need.
Aksh Dantal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.