Kre*_*ade 1 python http urllib
我正在通过向python发送请求来测试我的网页软件.我能够发送请求,接收响应并解析json.但是,网页上的一个选项是下载文件.我发送下载请求,可以确认响应头包含我期望的内容(application/octet-stream和相应的文件名),但Content-Length为0.如果长度为0,我认为文件实际上并未发送.我能够从其他方式下载文件,所以我知道我的软件可以工作,但我无法使用python.
我建立了请求然后做:
f = urllib.request.urlopen(request)
f.body = f.read()
Run Code Online (Sandbox Code Playgroud)
我希望数据在f.body中,但它是空的(我看到"b"')
有没有不同的方法从python中的附件访问文件内容?
有没有不同的方法从python中的附件访问文件内容?
这是蟒蛇,请求代替urllib,因为我更熟悉.
import requests
url = "http://example.com/foobar.jpg"
#make request
r = requests.get(url)
attachment_data = r.content
#save to file
with open(r"C:/pictures/foobar.jpg", 'wb') as f:
f.write(attachment_data)
Run Code Online (Sandbox Code Playgroud)