其他分享
首页 > 其他分享> > 爬取狗狗图片

爬取狗狗图片

作者:互联网

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

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

标签:狗狗,group,imageInfo,filename,爬取,item,import,response,图片
来源: https://www.cnblogs.com/masg/p/16330100.html