Reputation: 1859
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.
Upvotes: 0
Views: 102
Reputation: 5560
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.
Upvotes: 1