python:attributeError:wendys.read和.text不起作用

发布于 2025-02-13 12:30:37 字数 952 浏览 0 评论 0原文

这是我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把昨日还给我 2025-02-20 12:30:37

您遇到两个错误的原因。

首先发生,因为类型requests.models.response requests.get返回没有函数read

发生第二个之所以发生,是因为当您使用选项“ WB”打开时,您无法将str键入文件键入;这表明您要使用“写二进制”模式打开文件,该模式仅接受类似字节的对象而不是字符串。

我看下载从url 下载返回的zip文件以对您的代码进行此修改:

import requests

feeds = map(str,range(2002, 2023))


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, stream=True)
    if response.status_code == 200:
        print("Success")
        with open(f"{feed}.zip", "wb") as f:
            for chunk in response.iter_content(chunk_size=512):
                if chunk:  # filter out keep-alive new chunks
                    f.write(chunk)

您可以查看替代方案的链接。

You are getting two errors with different causes.

The first occurs because the type requests.models.Response that requests.get returns does not have a function read.

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:

import requests

feeds = map(str,range(2002, 2023))


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, stream=True)
    if response.status_code == 200:
        print("Success")
        with open(f"{feed}.zip", "wb") as f:
            for chunk in response.iter_content(chunk_size=512):
                if chunk:  # filter out keep-alive new chunks
                    f.write(chunk)

You could take a look at the link for alternatives.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文