python:attributeError:wendys.read和.text不起作用
这是我的代码:
import requests
feeds = []
for i in range(2002, 2023):
feeds.append(str(i))
for feed in feeds:
link = f"https://nvd.nist.gov/feeds/json/cve/1.1/nvdcve-1.1-{feed}.json.zip"
response = requests.get(link)
if response.status_code == 200:
print("Success")
with open(f"{feed}.zip", "wb") as f:
f.write(response.read())
但是,当我在最后一行中使用Response.Read()
时,它给了我这个错误:
Traceback (most recent call last):
File "c:\Users\30kal\database.py", line 48, in <module>
f.write(response.read())
AttributeError: 'Response' object has no attribute 'read'
但是,当我尝试使用wenders.text.text
时,它给了我这个错误:
Traceback (most recent call last):
File "c:\Users\30kal\database.py", line 48, in <module>
f.write(response.text)
TypeError: a bytes-like object is required, not 'str'
有什么想法吗?
Here is my code:
import requests
feeds = []
for i in range(2002, 2023):
feeds.append(str(i))
for feed in feeds:
link = f"https://nvd.nist.gov/feeds/json/cve/1.1/nvdcve-1.1-{feed}.json.zip"
response = requests.get(link)
if response.status_code == 200:
print("Success")
with open(f"{feed}.zip", "wb") as f:
f.write(response.read())
However, when I use response.read()
on the last line, it gives me this error:
Traceback (most recent call last):
File "c:\Users\30kal\database.py", line 48, in <module>
f.write(response.read())
AttributeError: 'Response' object has no attribute 'read'
But, when I try to use response.text
, it gives me this error:
Traceback (most recent call last):
File "c:\Users\30kal\database.py", line 48, in <module>
f.write(response.text)
TypeError: a bytes-like object is required, not 'str'
Any idea why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您遇到两个错误的原因。
首先发生,因为类型
requests.models.response
requests.get
返回没有函数read
。发生第二个之所以发生,是因为当您使用选项
“ WB”
打开时,您无法将str
键入文件键入;这表明您要使用“写二进制”模式打开文件,该模式仅接受类似字节的对象而不是字符串。我看下载从url 下载返回的zip文件以对您的代码进行此修改:
您可以查看替代方案的链接。
You are getting two errors with different causes.
The first occurs because the type
requests.models.Response
thatrequests.get
returns does not have a functionread
.The second occurs because you can't write a
str
type to a file when you open it with the option"wb"
; that indicates you want to open the file with "write binary" mode, which only accepts bytes-like objects, not strings.I looked at Download Returned Zip file from URL to make this modification to your code:
You could take a look at the link for alternatives.