-1

I am struggling to get my head around this. If I scrape a value from a website for example a quantity number of a product, I will be running my python script until that quantity number will change and print the new quantity every time it changes.

Let's say the quantity is 50, if this goes to 51 or 49 my python script should print either increase or decrease. However this quantity '50' can't be predetermined as this value will keep changing. This is the part I am stuck on. I want it to keep running even if it increases/decreases to detect further changes. Right now I have this.

import requests

while True:

   s = requests.Session()
   req = s.get("url_here")
   quantityval = re.compile('quant:([0-9])')
   val = re.findall(quantityval,str(req.content))
   #val output = '50'

   quantity = (int(val))

   if quantity != '50':
      print ('Stock Change')
   else:
      print ('No Change, trying again...')

This will then only then use the '50' however I then want this 50 to be updated to the new number and so on.

1
  • So you need to store a quantity that varies? Have you tried creating a variable, e.g. last_quantity = 50 followed by last_quantity = quantity whenever a change is witnessed? Commented Apr 10, 2020 at 22:43

1 Answer 1

0

Keep track of the current value and replace it each time it changes.

import requests
last = 50
while True:

   s = requests.Session()
   req = s.get("url_here")
   quantityval = re.compile('quant:([0-9])')
   val = re.findall(quantityval,str(req.content))
   #val output = '50'

   quantity = (int(val))

   if quantity != last:
      print ('Stock Change')
      last = quantity
   else:
      print ('No Change, trying again...')
Sign up to request clarification or add additional context in comments.

1 Comment

I can't just keep changing the current value every minute or so. That's what I am trying to figure out

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.