网页爬取

1、图片爬取代码

import requests
import os

root = "H:/美图/"
url = "https://k.zol-img.com.cn/sjbbs/7692/a7691501_s.jpg"
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)
        r.raise_for_status()
        with open(path, "wb") as f:
            f.write(r.content)
            f.close()
            print("文件保存成功")
    else:
        print("文件已经存在")
except:
    print("爬取失败")

猜你喜欢

转载自www.cnblogs.com/nxrs/p/11031017.html