开发者

Python urllib2 sending a post

开发者 https://www.devze.com 2023-03-29 17:29 出处:网络
I want to open a page then find a number and multiply by another random number and then submit it to the page So what I\'m doing is saving the page as a html then finding the 2 numbers multiplying it

I want to open a page then find a number and multiply by another random number and then submit it to the page So what I'm doing is saving the page as a html then finding the 2 numbers multiplying it then sending it as a post but

post = urllib.urlencode({'answer': goal, 'submit': 'Submit+Answer'})
req2 = urllib2.Request("example", None, headers)
response = urllib2.urlopen(req, post) #this causes it not to work it opens the page a second time

this makes it connect a second time and开发者_StackOverflow thus the random number sent is wrong since it makes a new random number so how can i send a post request to a page I already have open without reopening it?


You might want to use something like mechanize, which enables stateful web browsing in Python. You could use it to load a URL, read a value from a page, perform the multiplication, place that number into a form on the page, and then submit it.

Does that sound like what you're trying to do? This page gives some information on how to fill in forms using mechanize.


I don't believe urllib supports keeping the connection open, as described here.

Looks like you'll have to send a reference to the original calculation back with your post. Or send the data back at the same time as the answer, so the server has some way of matching question with answer.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号