最简单的网络图片的爬取 --Pyhon网络爬虫与信息获取
生活随笔
收集整理的這篇文章主要介紹了
最简单的网络图片的爬取 --Pyhon网络爬虫与信息获取
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1、本次要爬取的圖片的url
http://www.nxl123.cn/static/imgs/php.jpg
2、代碼部分
import requestsimport 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("文件已經(jīng)存在");
except:
print("爬取失敗!")
3、打印結(jié)果
桌面上自動(dòng)新建的文件夾:
轉(zhuǎn)載于:https://www.cnblogs.com/qikeyishu/p/9354868.html
總結(jié)
以上是生活随笔為你收集整理的最简单的网络图片的爬取 --Pyhon网络爬虫与信息获取的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 7_23 day26 14min
- 下一篇: Educational Codeforc