python reptile learning (eight) regular expressions batch crawling sister pictures

Regular get a piece of quite a long time, a little bit to try

# -*- coding: utf-8 -*-
import requests
import re
import os
if __name__ == '__main__':
    #创建一个文件夹,保存所有图片
    if not os.path.exists('./MMLibs'):
        os.mkdir('./MMLibs')

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

    url='https://www.2717.com/tag/434.html'
    #使用通用爬虫对url对应的页面整张爬取
    page_text = requests.get(url=url,headers=headers).text
    #使用聚焦爬虫进行解析
    #正则表达式
    ex = '<li>.*?<img.*?src="(.*?)".*?</li>'
    #re.s 单行匹配 re.m多行匹配
    img_src_list= re.findall(ex,page_text,re.S)
    #print(img_src_list)
    for src in img_src_list:
        img_data = requests.get(url=src,headers=headers).content
        #生成图片名称
        img_name= src.split('/')[-1]
        #图片储存路径
        imgPath = './MMLibs/'+img_name
        with open(imgPath,'wb')as fp:
            fp.write(img_data)
            print(img_name,"下载成功")

python folder operation
to find the current path, create a new file folder storage conversion

All operations on the file folder to be added in front of './' or '/', such as './file1', '/ file1'.

file_path = os.getcwd()     #找到当前文件路径
file_name = "./pinyin"	#新文件夹名字 
isExists = os.path.exists(file_name)	#判断这个文件夹是否存在
if isExists:
    print(file_path + file_name + "目录已存在")

else:
    os.mkdir(file_path + file_name)
    print(file_path + file_name + "目录创建成功")

Here Insert Picture Description

Published 23 original articles · won praise 0 · Views 673

Guess you like

Origin blog.csdn.net/haimian_baba/article/details/103732703