栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

爬虫抓取图片(爬虫图片下载)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

爬虫抓取图片(爬虫图片下载)

import requests
import os
import urllib.request as ur
from bs4 import BeautifulSoup
import re
hd=ur.urlopen("https://baidu.com/")
cat=hd.read().decode("utf-8")
soup=BeautifulSoup(cat,"html.parser")
soup1=soup.find_all(re.compile("img"))
j=0
for i in soup1:
    j=j+1
    str1=i.get("src")
    print(j)
    root = "D://tppq//"
    url = str1
    path = root + url.split('/')[-1]
    kv={"user-agent":"Mozilla/5.0"}
    cookie={}
    try:
        if not os.path.exists(root):
            os.mkdir(root)
        if not os.path.exists(path):
            r = requests.get(url,headers=kv,cookies=cookie)
            with open(path, 'wb') as f:
                f.write(r.content)
                f.close()
                print("文件保存成功")
        else:
            print("文件已存在")
    except:
        print("爬取失败")

懒得写注释

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/772890.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号