• 爬取狗狗图片


    最近小朋友喜欢狗狗,一个个下载太慢了,还得知道叫什么名字,安排

    import requests
    import json
    import re
    from PIL import Image, ImageDraw, ImageFont
    
     
    url = 'https://www.bagong.cn/dog/'  
    response = requests.get(url)
    
    decodestr = response.text
      
    aIter = re.finditer('<div class="fff"[\s\S]*?src="([\s\S]*?)"[\s\S]*?</div>[\s\S]*?<h4>([\s\S]*?)</h4>[\s\S]*?</div>',decodestr)
    for item in aIter:
        print(item.group(2),item.group(1))
        response = requests.get(item.group(1))
        filename = item.group(2)
        
        with open('dog/'+filename + ".jpg", "wb") as my:
            try:
                my.write(response.content)
                imageInfo = Image.open('dog/'+filename + ".jpg") 
                draw = ImageDraw.Draw(imageInfo)
                fontOne = ImageFont.truetype("‪C:\Windows\Fonts\simfang.ttf", 26)
                draw.text((2, imageInfo.size[1]-30), filename, fill=(255, 0, 0), font=fontOne,stroke_width=0)
                imageInfo.save('dog/'+filename + ".jpg")
            except:
                pass 
    

    效果图:

    image-20220531130950282

  • 相关阅读:
    nodejs
    Linux 的常用命令
    vue入门学习1_事件及修饰符
    mongo笔记
    spring boot笔记
    CentOS 6 ElasticSearch 5.5.x 安装指南
    linux用户和用户组
    linux下配置安装
    linux命令
    elasticsearch
  • 原文地址:https://www.cnblogs.com/masg/p/16330100.html
Copyright © 2020-2023  润新知