我想向应该使用.csv文件回复的HTTPS站点发送帖子请求。我有这个Python代码:
try:
#conn = httplib.HTTPSConnection(host="www.site.com", port=443)
=>给出BadStatusLine:''错误
conn = httplib.HTTPConnection("www.site.com");
params = urllib.urlencode({'val1':'123','val2':'abc','val3':'1b3'})
conn.request("POST", "/nps/servlet/exportdatadownload", params)
content = conn.getresponse()
print content.reason, content.status
print content.read()
conn.close()
except:
import sys
print sys.exc_info()[:2]
输出:
Found 302
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>302 Found</TITLE>
</HEAD><BODY>
<H1>Found</H1>
The document has moved <A HREF="https://www.site.com/nps/servlet/exportdatadownload">here</A>.<P>
<HR>
<ADDRESS>Oracle-Application-Server-10g/10.1.3.5.0 Oracle-HTTP-Server Server at mp-www1.mrco.be Port 7778</ADDRESS>
</BODY></HTML>
我做错了什么?我如何“捕获”返回的.csv文件? 我尝试使用Chrome Extentions(高级休息客户端,并且正在运行......)
的POST请求答案 0 :(得分:1)
带有302错误的HTML输出是因为您使用HTTP而不是HTTPS连接到网站,这位于您的代码中:
conn = httplib.HTTPConnection("www.site.com");
可能是因为您的代码中已注释掉的部分出现了其他错误,所以您正在这样做。
如果我尝试这样做,我会使用Requests,其中非常明确documentation。使用请求代码将类似于:
import requests
url = "https://www.example.com/nps/servlet/exportdatadownload"
payload = { "val1": "123", "val2": "abc", "val3": "1b3" }
r = requests.post(url, data=payload, verify=True)
CSV文件应该在r.content中,可以写入文件。