How to pass parameter to Url with Python urlopen

2019-05-22 23:34发布

问题:

I'm currently new to python programming. My problem is that my python program doesn't seem to pass/encode the parameter properly to the ASP file that I've created. This is my sample code:

import urllib.request

url = 'http://www.sample.com/myASP.asp'
full_url = url + "?data='" + str(sentData).replace("'", '"').replace(" ", "%20").replace('"', "%22") + "'"
print (full_url)
response = urllib.request.urlopen(full_url)
print(response)

the output would give me something like:

http://www.sample.com/myASP.asp?data='{%22mykey%22:%20[{%22idno%22:%20%22id123%22,%20%22name%22:%20%22ej%22}]}'

The asp file is suppose to insert the acquired querystring to a database.. But whenever I check my database, no record is saved. Though if I do copy and paste the printed output on my browser url, the record is saved. Any input on this? TIA

Update: Is it possible the python calls my ASP File A but it doesn't call my ASP File B? ASP File A is called by python while ASP File B is called by ASP File A. Because whenever I run the url on a browser, the saving goes well. But in python, no saving of database occurs even though the data passed from python is read by ASP File A..

回答1:

Use firebug with Firefox and watch the network traffic when the page is loaded. If it is actually an HTTP POST, which I suspect it is, check the post parameters on that post and do something like this:

from BeautifulSoup import BeautifulSoup
import urllib

post_params = {
              'param1' : 'val1',
              'param2' : 'val2',
              'param3' : 'val3'
              }
post_args = urllib.urlencode(post_params)

url = 'http://www.sample.com/myASP.asp'
fp = urllib.urlopen(url, post_args)
soup = BeautifulSoup(fp)

If its actually HTTP POST, this will work.



回答2:

In case anybody stumbles upon this, this is what I've come up with:

py file:

url = "my.url.com"
data = {'sample': 'data'}

encodeddata = urllib.parse.urlencode(data).encode('UTF-8')
req = urllib.request.Request(url, encodeddata)
response = urllib.request.urlopen(req)

and in my asp file, I used json2.js:

jsondata = request.form("data")
jsondata = replace(jsondata,"'","""")
SET jsondata = JSON.parse(jsontimecard)

Note: use requests instead. ;)



回答3:

First off, I don't know Python.

But from this : doc on urllib.request

the HTTP request will be a POST instead of a GET when the data parameter is provided

Let me make a really wild guess, you are accessing the form values as Request.Querystring(..) in the asp page, so your post wont pass any values. But when you paste the url in the address bar, it is a GET and it works.

just guessing, you could show the .asp page for further check.