python - 用python下载图像
问题描述
嗨,我想在此链接中下载图像,但无法打开创建的图像。这是输出文件 我该如何解决这个问题?这是代码:
from requests.models import Response
import requests
url = 'https://beta.mangaeden.com/it/it-manga/jojo-no-kimyou-na-bouken---jojorion/1/1'
r = requests.get(url)
soup = BeautifulSoup(r.content, 'html.parser')
immagine = soup.find(id='mainImg')
src = str(immagine.get('src'))
link_immagine = "https:"+src
print(link_immagine)
'''download imagege'''
response = requests.get(link_immagine)
f = open("C:\\Users\\chris\\Desktop\\image.jpg", "wb")
f.write(response.content)
f.close()
print('download successfull')
解决方案
该网站似乎403 Forbidden
根据您的用户代理返回错误。如果您使用浏览器的用户代理请求图像,它应该可以工作,例如:
response = requests.get(link_immagine, headers={
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0 Safari/537.36'
})
推荐阅读
- angular - 使用 Angular 在数据库中以不同的名称提交相同的文件
- cross-compiling - How to fix "Section .vectors overlaps section .data " | Adding ATmega1281 Support to Compiler
- mongodb - 添加身份验证后,docker mongo 丢失数据
- java - 获取安卓设备摄像头分辨率
- android - 无法更改 MotionLayout 中 TextInputLayout 的可见性
- php - 元值上的非法字符串偏移 === 1
- javascript - 我无法找到有关将我的状态传递给另一个文档的组件的文档/视频
- coq - 如何自动利用 x <> y 形式的假设?
- php - Yii 1 Yii::import 不起作用但包含 do
- javascript - 如何在 3-5 秒后停止加载动画?