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.