最简单的网络图片的爬取 --Pyhon网络爬虫与信息获取

1、本次要爬取的图片的url

http://www.nxl123.cn/static/imgs/php.jpg

最简单的网络图片的爬取 --Pyhon网络爬虫与信息获取

2、代码部分

import requests
import os
url = "http://www.nxl123.cn/static/imgs/php.jpg"
root = "C:/Users/Niuxi/Desktop/pic/"#注意最后“/”带上
path = root+url.split('/')[-1]
try:
if not os.path.exists(root):
os.makedirs(root)
if not os.path.exists(path):
r = requests.get(url)
# print(r.status_code)
with open(path,'wb') as f:
f.write(r.content)
f.close()
print("文件保存成功")
else:
print("文件已经存在");
except:
print("爬取失败!")

3、打印结果

最简单的网络图片的爬取 --Pyhon网络爬虫与信息获取

桌面上自动新建的文件夹:

最简单的网络图片的爬取 --Pyhon网络爬虫与信息获取

上一篇:How to compare dates in Java


下一篇:sublime福音:微信小程序组件及API补全插件