Skip to content

Instantly share code, notes, and snippets.

@benninkcorien
Created October 10, 2020 14:02
Show Gist options
  • Save benninkcorien/96ef34f7fc5169ad18ca820bfc73a3a9 to your computer and use it in GitHub Desktop.
Save benninkcorien/96ef34f7fc5169ad18ca820bfc73a3a9 to your computer and use it in GitHub Desktop.
Get average price of sold items from Ebay
import sys
import requests
import os
import re
from urllib.request import urlopen
from bs4 import BeautifulSoup
# changed script from
# https://www.reddit.com/r/declutter/comments/j6ts24/python_script_to_calculate_average_sell_price_of/
# Install Python 3.x from https://www.python.org/downloads/
# open cmd.exe with Administrator rights
# > pip3 install beautifulsoup4
# Go to Ebay
# Search for your item.
# Click "Advanced",and check the "Sold" box to only get sold items
# Copy the URL
# Open Command Prompt/Terminal (cmd.exe on Windows)
# Go to the folder where this script is saved.
# Example
# > cd C:\Downloads
# Type in
# > python ebay_scraper.py
# Paste in your URL when it asks for it
url = input("Enter Ebay URL: ")
# go to URL
r = requests.get(url)
# check for <Response 200>
""" if str(r) != '<Response [200]>':
print("Problem: ", r)
else:
print("Connection Successful - Received <Response [200]>") """
# parse content through BeautifulSoup
soup = BeautifulSoup(r.content, 'lxml')
# Write soup to text file so you can check it if something goes wrong
# with open('soupfile.txt', "w") as f:
# f.write(str(soup))
# f.close()
# scrape class_="bidsold" from search page
prices = []
for price in soup.find_all("span", class_="bold bidsold"):
# print(type(price.text), price.text)
if "Trending at" not in price.text:
if "to" not in price.text:
clean_price = price.text.strip()
prices.append(clean_price)
# print(clean_price)
# determine average sold price of item
avg_price = []
# the currency is whatever non-digits come before the digit in the price
firstprice = prices[0]
currency = re.search('^\D+', firstprice).group(0)
# print("Currency is: " + str(currency))
for p in prices:
# Strip currency, replace , with . (for EUR etc)
p = p.strip(str(currency))
p = p.replace(',', '.')
fprice = float(p)
avg_price.append(fprice)
average_price = sum(avg_price) / len(avg_price)
print( "The average price is " + currency + str(round(average_price, 2)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment