如何通过参数和Python的urlopen到网址 [英] How to pass parameter to Url with Python urlopen

查看:480
本文介绍了如何通过参数和Python的urlopen到网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前新Python编程。我的问题是,我的Python程序似乎并没有正确地传递/ EN code中的参数设置为我创建了ASP文件。这是我的样本code:

 进口urllib.request里URL ='http://www.sample.com/myASP.asp
full_url = URL +?数据='+ STR(sentData).replace(',').replace(,%20)。REPLACE('',%22)+ '
打印(full_url)
响应= urllib.request.urlopen(full_url)
打印(响应)

输出会给我是这样的:

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

ASP文件是假设插入查询字符串获取到数据库。但每当我检查我的数据库中,没有记录被保存。但如果我复制和粘贴在我的浏览器URL打印输出,保存记录。在这个任何输入? TIA

更新:
是否有可能蟒蛇叫我的ASP文件,但它不叫我的ASP文件B? ASP文件是由蟒蛇而ASP文件B是由ASP文件答:因为每当我在浏览器中运行的网址,节约顺利称为调用。但是在Python,即使从蟒蛇传递的数据是由ASP文件的读未发生数据库节能..


解决方案

使用Firebug与Firefox和加载页面时,观看网络流量。如果它实际上是一个HTTP POST,我怀疑它是,检查该职位岗位参数做这样的事情:

 从BeautifulSoup进口BeautifulSoup
进口的urllibpost_params = {
              '参数1':'VAL1',
              '参数2':'val2的,
              参数3':'VAL3
              }
post_args = urllib.urlen code(post_params)URL ='http://www.sample.com/myASP.asp
FP =了urllib.urlopen(URL,post_args)
汤= BeautifulSoup(FP)

如果其实际HTTP POST,这将正常工作。

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

解决方案

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.

这篇关于如何通过参数和Python的urlopen到网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆