I’m trying to scrape sold items on eBay. I’m trying to scrape:
Here is my code where I load in html code and convert to soup object:
ebay_url = 'https://www.ebay.com/sch/i.html?_from=R40&_nkw=oakley+sunglasses&_sacat=0&Brand=Oakley&rt=nc&LH_Sold=1&LH_Complete=1&_ipg=200&_oaa=1&_fsrp=1&_dcat=79720'
response = requests.get(ebay_url)
soup = bs(response.text, 'html.parser')
#print(soup.prettify())
I’m working on getting the titles, prices, and date sold and then loading it into a csv file. Here is the code I have for the titles:
title = soup.find_all("h3", "s-item__title s-item__title--has-tags")
print(title)
listing_titles = []
for i in range(1,len(title)):
listing_titles.append(title[i].text)
print(listing_titles)
Which just returns empty square braces like []. The html soup object prints correctly, and the response prints as 200. It seems that my code should work, and that finding the post price and sale date should be similar. I’m wondering if this is a job for selenium. Hopefully someone can help! Thanks!
2
Answers
First you can find all div based on class and loop over it get title,price and date
Output:
The response may be empty because the
requests
request may be blocked, since the defaultuser-agent
in therequests
library ispython-requests
to tell the website that it is a bot or script that is sending the request. Check what user agent you have.An additional step besides providing browser user-agent could be to rotate
user-agent
, for example, to switch between PC, mobile, and tablet, as well as between browsers e.g. Chrome, Firefox, Safari, Edge and so on.It is also possible to fetch all results from all pages using pagination, the solution to this would be to use an infinite
while
loop and test for something (button, element) that will cause it to exit.In our case, this is the presence of a button on the page (
.pagination__next
selector).Check code in online IDE.
Output:
file is created: "ebay_products.csv"
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:
Output:
file is created: "ebay_products.csv"
There’s a 13 ways to scrape any public data from any website blog post if you want to know more about website scraping.