How can I use python to load a browser session that posts values to a url?
I have a python script that takes a number of variables. I also have a html page that can receive post values.
How can I start a browser from python and point it to the html page I have above and send those post variables to the html url?
The problem I have is that if I use urllib/urllib2 to do the post, it doesn't load the browser window. And if I want to load a browser window I cannot send a post to the url开发者_如何学编程.
This is how you can do a POST to a url but it doesn't open up a browser, instead it can receive back values like so. But I do not need to read back values, I need to open a Internet browser, point it to a specific url and post the variables to that url.
data = urllib.urlencode({"fileTitle" : "ThisFileName", "findtype" : "t", "etc" : "etc"})
f = urllib.urlopen("http://www.domain.com/someurl/", data)
# Read the results back.
s = f.read()
s.close()
Do the values have to be supplied via HTTP POST? Could you supply the parameters as part of the URL and use a GET instead?
e.g. Build a URL similar to the following:
http://somehost/somefile.html?param1=value1¶m2=value2&...
"... it doesn't load the browser window."
Sorry, I don't fully understand what you try to do. But maybe Selenium Remote Control (RC) is interesting for you. You can write scripts to remote controll a browser. It's used for automated testing. Python is supported as well. You can make the browser to submit forms or click on links eg. to PHP scripts or whatever.
This seems to be the only way to get what I want done.
You can use urllib to call a page on the server that stores the data (either in sql table or in a temporary file) and returns an unique id. Then you can use the unique id to fetch that data from its known source.
The python code will look something like this:
import urllib
data = urllib.urlencode({"postField1" : "postValue1", "postField2" : "postValue2", "etc" : "etc"})
f = urllib.urlopen("http://www.domain.com/storePostData.php", data)
# At this point your storePostData.php file stores all the post
# info in either an sql DB or temporary file so this can accessed later on and
# an uuid is passed back which we now read below. In may case I store all post
# fields in a sql DB and each column represents each post field.
uuid = f.read()
# the uuid is the sql table id field which is auto_incremented.
# SO now we load the default browser below and send it the uuid so the php script
# can access the sql data. Once it has been accessed and the form fields have been
# received then we delete that row as the information is useless to us now that we
# have filled in the forms fields
import webbrowser
webbrowser.open_new("http://www.domain.com/someOtherUrl?uuid=" + uuid)
All the rest of the work is done by PHP scripting, where it stores the info, and then retrieves the info based on the uuid. I'm not going to put an example of the code up here as that's just general knowledge and all I'm trying to do is get the concept through here.
I hope this helps someone.
Cheers
精彩评论