Using Python to generate product in-stock notifications
No ‘Notify Me’ Button? No Problem!
It’s not too often that I need to buy new shoes, but I was in the market for a pair of shoes for the “athletic casual” niche. I had found a pair I wanted in a cool blue color. I came back to the website the next day, but the blue was no longer an option! Now, when a color option is in stock in some sizes, you can click the “Sold Out” option and use the handy “Notify Me” button to send you an email when your size is back in stock.
However, what if all of the sizes of that color are out? The thumbnail image of that color option disappears and there is no way to receive the notification. You’ll just have to refresh the page until it appears…unless we could program a script to let us know when the color is back in stock. In this article, I’ll show how I used BeautifulSoup to scrape the website and the Gmail SMTP server to notify me when the shoes were back in stock.
The first thing I did was set up the email notification for further down in the script when it would be utilized:
With that out of the way, it was time to start parsing through the site contents to see where we could pull valuable information for determining our in-stock indicator. A good tip for what to start targeting with BeautifulSoup is right-clicking the text or object of interest and clicking “Inspect” (in Chrome or Brave) to see which HTML tags you should use to search.
In my case, the updated shoe information was loaded in dynamically with JavaScript. By pulling the page content and searching for a shoe color that was in stock, I was able to locate the region that would be useful in determining stock status. Below is a subset of the returned text that had the right type of information I was looking for contained in a script tag.
From there, I was able to refine the BeautifulSoup search by identifying only script tags with that type. Since there were a finite number of results of the script tag, I indexed to the one I wanted.
This is pretty close, but now we’d like to parse the output from our “data” variable as a python dict. We can do this with a bit of cleaning to remove the script tags and convert the string text to a Python dictionary.
Result:
{'id': '5836', 'label': 'Deep Sea Blue', 'products': ['27332', '27314', '27329', '27311', '27326', '27323', '27338', '27320', '27335', '27317']}
The “products” key-value pair tells us if any of that color are in stock. If there are none in stock, an empty list is returned. Unfortunately, the number strings don’t have any pattern and seem to be unique to the size and color combination, so I can’t use a specific string of numbers to inform me of sizes in stock. However, a populated versus empty list is enough to give us a notification for at least some sizes in stock.
With the script complete, I used Windows Task Scheduler to run the script once a day. As soon as the color is in stock, I receive an email informing me of the in-stock status.
To set up the script to automatically run as a task, I first generated a .bat file for the script. Once you’ve set up the .bat file, click into your start menu and type “Task Scheduler.” On the right-hand side, click “Create Basic Task.” Give the task a name and description. Click Next
In the Trigger tab, select how frequently you want the task to run. I selected daily for this particular script. In the Action tab, select the radio button for “start a program” and click next. In the next prompt, enter the location of the script file and add in the root folder of the script in the “Start in” section.
Click next to finish creating the task. Congrats, you’re done! In the main Task Scheduler window, you should now see your new task populated in the library of other tasks. With the script selected, on the right-hand side of the window, you can also click “Run” to test it out and make sure it functions as expected.
Here’s an example of what I receive in my email once the shoes are in stock and the script is successfully executed.
Thanks for reading! The code can be found on my github here.