python爬虫学习笔记三:图片爬取

版权声明:关注微信公众号:摸鱼科技资讯,联系我们 https://blog.csdn.net/qq_36949176/article/details/84197525

图片爬取的代码

r.content 文件的二进制格式

Python引入了with语句来自动帮我们调用close()方法:

open(path, ‘-模式-‘,encoding=’UTF-8’) 

w:以写方式打开,
a:以追加模式打开 (从 EOF 开始, 必要时创建新文件)
r+:以读写模式打开
w+:以读写模式打开 (参见 w )
a+:以读写模式打开 (参见 a )
rb:以二进制读模式打开
wb:以二进制写模式打开 (参见 w )
ab:以二进制追加模式打开 (参见 a )
rb+:以二进制读写模式打开 (参见 r+ )
wb+:以二进制读写模式打开 (参见 w+ )
ab+:以二进制读写模式打开 (参见 a+ )fp.read([size])                    

 语法链接https://blog.csdn.net/u011389474/article/details/60140311

import requests
import os
url="http://moyu.studiosworks.cn/img/wechat.jpg"
root="D://pics//"
path=root+url.split('/')[-1]
try:
    if not os.path.exits(root):
        os.mkdir(root)
    if not os.path.exits(path):
        r=requests.get(url)
        with open(path,'wb') as f:
            f.write(r.content)
            f.close()
            print("文件保存成功")
    else:
        print("文件已存在")
except:
    print("爬取失败")

猜你喜欢

转载自blog.csdn.net/qq_36949176/article/details/84197525