Amazon Price Checker (Part 3)

Version 3 of my Amazon.co.uk price checker utilises the JSON (JavaScript Object Notation) module so that JSON files can be used by Python. I’ve stored the ASIN (Amazon IDs) of the Amazon products that I want to check the price of in a JSON file (called amazlist.json) and Python now calls on that file.

I’ve predefined the “spends” variable as 30 whilst I’m automating the script.

———————

#Amazon.co.uk Price Checker V3 / geektechstuff

import bs4, requests, json

#Import the Beautiful Soup and Requests modules

headers = {‘User-Agent’: ‘Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/604.5.6 (KHTML, like Gecko) Version/11.0.3 Safari/604.5.6’}

#Needed a headers otherwise Amazon.co.uk fails to give any details

#spends=input(‘How much spends in £ is there?:’)

spends=30

spends=float(spends)

filename=’amazlist.json’

with open(filename) as f:

    asin_data=json.load(f)

    for asin in asin_data:

        asintxt=str(asin)

        head,sep,tail=asintxt.partition(‘<‘)

        asintxt2=tail

        head,sep,tail=asintxt2.partition(‘>’)

        asintxt3=head.strip()

        print(asintxt3)        

        url = ‘http://www.amazon.co.uk/dp/’+asintxt3     

        res=requests.get(url,headers=headers)

#Requests the URL and uses the headers to make the script “look” like a web browser

        res.raise_for_status()

        soup=bs4.BeautifulSoup(res.text,”html.parser”)

#Needed the html.parser line otherwise Beautiful Soup gives a warning

        asidprice=soup.select(‘#priceblock_ourprice’)

        asidtitle=soup.select(‘#productTitle’)

        if asidprice == []:

            print(‘Error’)

        else:

            ttext=str(asidtitle)

            head,sep,tail=ttext.partition(‘>’)

            ttext2=tail

            head,sep,tail=ttext2.partition(‘<‘)

            title=head.strip()

            print(title)

        

            ptext=str(asidprice)

            head,sep,tail=ptext.partition(‘£’)

            ptext2=tail

            head,sep,tail=ptext2.partition(‘<‘)

            price=head

            print(‘£’+price)

            price=float(head)

            if price > spends:

                print(“Still too expensive”)

            else:

                print(“Its cheap enough to purchase”)        

#turns the spanIDs into strings and then removes HTML that I don’t need to print

    

2 responses to “Amazon Price Checker (Part 3)”

  1. Pollen Check V1 (Python) – Geek Tech Stuff Avatar

    […] I fired up PyCharm and got to work on my Python program. Using the knowledge I gained writing my Amazon Price Check program, I instantly fell back to BeautifulSoup and got that to the do the hard […]

    Like

  2. Web Scrapping Part 1 (Python) – Geek Tech Stuff Avatar

    […] have briefly used web scrapping in some of my earlier Python work (Amazon price checker, pollen count) and I’m hoping to now expand that work/knowledge and go a bit more in […]

    Like

Welcome to GeekTechStuff

my home away from home and where I will be sharing my adventures in the world of technology and all things geek.

The technology subjects have varied over the years from Python code to handle ciphers and Pig Latin, to IoT sensors in Azure and Python handling Bluetooth, to Ansible and Terraform and material around DevOps.

Let’s connect