正则抓图

正则好难wow

import re
import os
import requests
# https://i2.hoopchina.com.cn/blogfile/202010/1/BbsImg_272354755380755_1601524071_s_51060_o_w_640_h_427_48708.jpg?x-oss-process=image/resize,w_800/format,webp
url='https://www.qiushibaike.com/imgrank/page/%d/'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2626.106 Safari/537.36'
}

if not os.path.exists('./嗅事百科图片'):
    os.mkdir('./嗅事百科图片')
for pagenum in range(1,3):
    new_url=format(url%pagenum)
    page_text=requests.get(url=new_url,headers=headers).text
    ex = '<div class="thumb">.*?<img src="(.*?)" alt.*?</div>'
    img_list=re.findall(ex,page_text,re.S)
    for src in img_list:
        src='https:'+src
        img_data=requests.get(url=src,headers=headers).content
        img_name=src.split('/')[-1]
        img_path='./嗅事百科图片/'+img_name
        with open(img_path,'wb') as fp:
            fp.write(img_data)
            print(img_name,'下载成功')

原文地址:https://www.cnblogs.com/Hsiung123/p/13811916.html