Skip to content

Instantly share code, notes, and snippets.

Last active April 30, 2021 20:57
What would you like to do?
Python script to extract a price from a product web page
from bs4 import BeautifulSoup
from urllib2 import Request, urlopen
import decimal
def findPrice(url, selector):
userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36"
req = Request(url, None, {'User-Agent': userAgent})
html = urlopen(req).read()
soup = BeautifulSoup(html, "lxml")
return decimal.Decimal([0].contents[0].strip().strip("$"))
print findPrice("", "#priceRow > div:nth-of-type(2)")
# Below is an example of how you could potentially use this to extract a price from an Amazon product web page
# print findPrice("", "#newOfferAccordionRow .header-price")
Copy link

How to run this example:

1. Install dependencies
apt-get install -y wget python python-bs4
2. Get the script from the gist
3. Run the script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment