Python批量爬取图片并下载到本地

import re
import requests
import os

def download(html):
    path = r'C:/Users/Administrator/Desktop/py/get_imgs/images'
    # 通过正则匹配
    pic_url = re.findall('<img .*? data-src="(.*?)" .*? \/>', html, re.S)
    title = re.findall('<h2 class="rich_media_title" id="activity-name">(.*?)<\/h2>', html, re.S)
    if title:
        title = title[0].strip()
    else:
        title = 'images'

    i = 1
    if not os.path.exists(path + './' + title):
        os.mkdir(path + './' + title)
    else:
        print("已下载完毕")
        return

    for key in pic_url:
        print("开始下载图片:" + key + "\r\n")
        try:
            pic = requests.get(key, timeout=10)
        except requests.exceptions.ConnectionError:
            print('图片无法下载')
            continue
        # 保存图片路径
        dir = path+'/'+title+'/'+str(i) + '.jpg'
        fp = open(dir, 'wb')
        fp.write(pic.content)
        fp.close()
        i += 1


def main():
    url = 'https://你猜.com/s/T7wamHFT0jp0Rdedjr6acA'
    result = requests.get(url)
    download(result.text)


if __name__ == '__main__':
    main()

本文由 来鹏飞 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

只有地板了

  1. Pen
    Pen

    +1

添加新评论