vendredi 3 août 2018

Web Scraping Out of Stock Notifier in Python with BeautifulSoup

I am trying to build a notifier that will send me an email when an out of stock item is back in stock. So far I have narrowed down how to retrieve the item name and price. However when I tried to use the blue button "Out of stock" next to "Find in store" as a tag, for some reason it retrieves "Add in cart" which appears only when the item is in stock. So I tried using the "OUT OF STOCK" text above the title to set oos_status to True. It's supposed to show as True according to if the i7 configuration on the webpage is Out of Stock, which it was at the time of this writing, but still shows up as False which means it is in stock. Improvements would be greatly appreciated. Here is the code:

from urllib.request import Request, urlopen
from bs4 import BeautifulSoup as soup 

url = 'https://www.microsoft.com/en-ca/p/huawei-matebook-x-pro- 
laptop/8n4k86d4j006/4X0P?activetab=pivot%3aoverviewtab'

req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
uClient = urlopen(req)
page_html = uClient.read()
uClient.close()

# html parsing
page_soup = soup(page_html, 'html.parser')

# grabs OOS container
# container_oos = page_soup.findAll("div", {"class": "cli_badge context-buy- 
box-badge"})
container_oos = page_soup.findAll('div', {"class": "cli_badge context-buy- 
box-badge"})

# grabs price container
container_price_disclaimer = page_soup.findAll("div", {'class': "price- 
disclaimer"})

# grabs name container
container_name = page_soup.findAll("div", {"class": "m-product-detail-hero- 
product-placement oneui-override"})

# finds text of name, price and out of stock status
name = container_name[0].findAll('h1', {'id': 'page-title'})[0].text.strip()
price = container_price_disclaimer[0].findAll('span')[0].text.strip()
oos_status = False

# Using 'OUT OF STOCK' text above title to decide whether out of stock
if container_oos[0].find('span', {'id': 'out-of-stock-badge'}) == None:
    oos_status = False
elif container_oos[0].find('span', {'id': 'out-of-stock-badge'}) 
[0].text.strip() == 'OUT OF STOCK':
    oos_status = True

Aucun commentaire:

Enregistrer un commentaire