How to pass parameter to Url with Python urlopen

浪尽此生 提交于 2019-12-06 11:21:45

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.

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. ;)

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!