我正在尝试从NASS Case Viewer下载一些图像。一个例子是
本例的图像查看器的链接是
我想这可能是不可见的,因为https。然而,这只是正面第二幅图像。在
图像的实际链接是(或者应该是?)在
这将简单地下载aspx二进制文件。在
我的问题是我不知道如何将这些二进制文件存储到正确的jpg文件中。在
我尝试过的代码示例是
import requests
test_image = "https://www-nass.nhtsa.dot.gov/nass/cds/GetBinary.aspx?Image&ImageID=497001669&CaseID=149006692&Version=1"
pull_image = requests.get(test_image)
with open("test_image.jpg", "wb+") as myfile:
myfile.write(str.encode(pull_image.text))
但在jpg文件中不正确。我还检查了pull_image.raw.read()
,发现它是空的。在
有什么问题吗?我的网址不合适吗?我使用beauthoulsoup将这些url放在一起,并通过检查几个页面中的HTML代码来查看它们。在
我是否错误地保存了二进制文件?在
我想跟进@t.m.adam的回答,为那些有兴趣在自己的项目中使用这些数据的人提供一个完整的答案。在
下面是我的代码,用于提取案例ID示例的所有图像。这是一个相当不干净的代码,但我认为它提供了您开始可能需要的东西。在
.text
将响应内容解码为字符串,因此您的imge文件将损坏。相反,您应该使用保存二进制响应内容的^{
^{pr2}$.raw.read()
也返回字节,但是为了使用它,必须将stream
参数设置为True
。在相关问题 更多 >
编程相关推荐