Created
June 26, 2020 22:45
-
-
Save MohamedRajabMohammed/07d15fd3d8d606e8f0ddcaa67198d378 to your computer and use it in GitHub Desktop.
Function to extract products details.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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