0

I have a link which is generating an equation which must be answered. To do that I'm trying to read returned http to find those numbers

import urllib2, urllib
url = 'http://someurl.com'
answerRegexp = re.compile('''(\d+)\s([+-])\s(\d+)\s=\s<input name="answer"''')
response = urllib2.urlopen(url)
html = response.read()
res = answerRegexp.findall(html)[0]

values = {'field1': 'value1',
      'field2': 'value2',
      'answer': eval("%s%s%s" % (res[0], res[1], res[2])) # 2+3 for example
}

data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()

This does not work since data is creating another request in which a new equation is being generated, thus the calculated value will not match the correct answer.

1 Answer 1

1

The website you are working with may be using session cookies which you need to pass back and forth in order to identify the previous session (and therefore preventing a new challenge to be generated). Consider using the Requests library http://docs.python-requests.org/en/latest/ which will take care of all of those for you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.