Skip to content

Instantly share code, notes, and snippets.

@MohamedRajabMohammed
Created June 26, 2020 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MohamedRajabMohammed/07d15fd3d8d606e8f0ddcaa67198d378 to your computer and use it in GitHub Desktop.
Save MohamedRajabMohammed/07d15fd3d8d606e8f0ddcaa67198d378 to your computer and use it in GitHub Desktop.
Function to extract products details.
def get_product_details(url):
headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"
}
details = {"name": "", "price": 0, "deal": True, "url": ""}
_url = extract_url(url)
if _url == "":
details = None
else:
page = requests.get(url, headers=headers)
soup = BeautifulSoup(page.content, "html5lib")
title = soup.find(id="productTitle")
price = soup.find(id="priceblock_dealprice")
if price is None:
price = soup.find(id="priceblock_ourprice")
details["deal"] = False
if title is not None and price is not None:
details["name"] = title.get_text().strip()
details["price"] = get_converted_price(price.get_text())
details["url"] = _url
else:
return None
return details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment