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

批量爬取网站图片-“优美库”篇(爬虫实战)

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

批量爬取网站图片-“优美库”篇(爬虫实战)

本次爬取运用到了requests,bs4的beautifulsoup好,re以及time模块,用了常用的方法提取到源代码的关键信息,最后获取每个信息的请求链接,从而获得我们的图片。

import requests

from bs4 import BeautifulSoup

import re

import time

url='https://www.''此处为需要获取的网站.cc/bizhitupian/'

res=requests.get(url)

obj=re.compile(r'

  • .*?)">

    res.encoding='uft-8'

    res.close()

    res=res.text

    res=BeautifulSoup(res,'html.parser')

    res=res.find_all('div',class_="pic-box")

    res=str(res).strip('[]')

    result=obj.finditer(res)

    for it in result:

        url1='https://www.umei.cc'+it.group('http')

        response = requests.get(url1)

        response.encoding='utf-8'

        response=response.text

        response=BeautifulSoup(response,'html.parser')

        name=response.find('title')

        name=str(name)

        name=name.strip('')

        img=response.find('img')

        img = img.get('src')

        rs=requests.get(img).content

        with open('img/'+name+'.jpg','wb')as f:

            f.write(rs)

        print('完成了',name)

        time.sleep(2)#怕请求太过频繁造成不必要的影响,所以设置了一定的延迟

    print("全部完成拉!")

  • 今天分享的内容就这些啦,希望大家有不好的地方可以提出来,帮我指正指正!

     (效果图片)

     

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

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

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