【问题标题】:Read files from the internet?从互联网上读取文件?
【发布时间】:2023-03-19 07:03:01
【问题描述】:

当我检索一个 URL(通过向 random.cat 发送请求)时:

print('Importing REQUESTS')
import requests
import json
import urllib
response = (requests.get("http://random.cat/meow"))
response = str(response.content)
print(response)
response = response.replace("b'","")
response = response.replace("'","")
response = response.replace("\\","")
print(response)
data = json.loads(response)
print (data["file"])`

然后我尝试使用以下命令打开它:

with open(line, 'rb') as f:
    print("work")`

我收到此错误:

Traceback (most recent call last):
  File "G:/catimages.py", line 21, in <module>
    with open(line, 'rb') as f:
OSError: [Errno 22] Invalid argument: 'http://random.cat/i/8Vilp.jpg' `

有什么想法吗?

【问题讨论】:

    标签: python-3.x windows-7


    【解决方案1】:

    open 无法直接打开 URL。您需要先使用请求来下载文件。见How do I download a file over HTTP using Python?

    编辑:需要明确的是,您提供的错误消息表明line 的值是'http://random.cat/i/8Vilp.jpg',因此您尝试调用open('http://random.cat/i/8Vilp.jpg', 'rb') 将不起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-08
      相关资源
      最近更新 更多