遍历https://logopond.com,下载所有图标,存放至文件夹

本文介绍了一种使用Python批量下载Logopond网站上图标的方法,通过解析网页源代码,提取图片链接并保存到本地文件夹。代码实现了翻页、异常处理及文件管理功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个爬的就是有点过分了,基本是网页https://logopond.com上的图标都会下载

import requests
from requests.exceptions import RequestException
import re
import os

def get_one_page(url):
    try:
        response = requests.get(url)
        if response.status_code == 200:
            return response.text
        return None
    except RequestException:
        return None

def DownPage(file_name, photo_html, num):
        try:
            r = requests.get(photo_html)
            if r.status_code == 200:
                with open(os.getcwd() + '/' + file_name + '/' + str(num) + '.png', 'wb') as f:
                    f.write(r.content)
                    f.close()
                    num += 1
                    return num
            else:
                print('Error')
                return None
        except RequestException:
            return None

def DeatleFile(file_name):
    path = os.getcwd() + '\\' + file_name
    for i in os.listdir(os.getcwd()):
        if file_name in i:
            for ii in os.listdir(path):
                os.remove(path + '\\' + ii)
            os.removedirs(path)

def CreatFile(file_nam):
    os.makedirs(os.getcwd() + '\\' + file_nam)

def main():
    file_nam = 'photo'
    DeatleFile(file_nam)
    CreatFile(file_nam)
    i = 1
    num = 0
    a = 0
    while 1:
        url = 'https://logopond.com/gallery/list/?gallery=featured&filter=&month=&year=&page=' + str(i)
        html = get_one_page(url)
        print(url)
        pattern = re.compile('<a href.*?img src="(.*?)".*?./a>', re.S)
        try:
            items = re.findall(pattern, html)
        except TypeError:
            i += 1
            continue
        for photo_html in list(items):
            if 'https://logopond.com' not in photo_html:
                photo_html = 'https://logopond.com' + photo_html
            else:
                continue
            a = DownPage(file_nam, photo_html, num)
            if None != a:
                num = a
            else:
                continue
        i += 1

if __name__ == '__main__':
    main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值