#图片爬取全代码,名字为网上原定义的名字
import requests
import os
url="https://gss3.bdstatic.com/-Po3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike116%2C5%2C5%2C116%2C38/sign=b93057c84a90f60310bd9415587bd87e/4d086e061d950a7b31ba953d0ad162d9f3d3c9b8.jpg"
root="D://PythonDemo//"
path=root+url.split('/')[-1]
try:
if not os.path.exists(root):
os.mkdir(root)
if not os.path.exists(path):
r=requests.get(url)
with open(path,'wb') as f:
f.write(r.content)
f.close()
print("文件保存成功")
else:
print("文件已存在")
except:
print("爬取失败")
Spider a picture
最新推荐文章于 2025-07-08 17:28:41 发布
本文介绍了一个简单的图片爬取程序,使用Python的requests库从指定URL下载图片并保存到本地目录。示例代码展示了如何处理路径、请求图片以及异常情况。
455

被折叠的 条评论
为什么被折叠?



