爬取iconfont的图标列表

作者: gavin 分类: Python,爬虫技术 发布时间: 2022-02-12 22:12

项目中需要搜索指定行业类型的icon,但iconfont不支持按行业搜索,于是把分页列表都爬取下来根据名称全局检索

import ssl
import requests as requests
f = "lucky.txt"
# 1.声明不验证ssl证书
ssl._create_default_https_context = ssl._create_unverified_context
a = 1
with open(f,"a") as file:
    for i in range(1, 1005):
        res=requests.get("https://www.iconfont.cn/api/collections.json?type=3&sort=time&limit=9&page=%d"%(int(a)))
        res.encoding = 'utf-8'
        a += 1
        novle = res.json()
        v = novle['data']['lists']
        print("=============================")
        print("======      第%d页:      ======"%(int(a)))
        print("=============================")
        file.write("=============================\n")
        file.write("======      第%d页:      ======\n" % (int(a)))
        file.write("=============================\n")
        for ii in v:
            #print(ii)
            print('https://www.iconfont.cn/collections/detail?cid=%d'%(int(ii['id'])))
            print("name:" + ii['name'])
            #print('description:' + ii['description'])
            #print('slug:' + ii['slug'])
            print('-------------------------')
            file.write('https://www.iconfont.cn/collections/detail?cid=%d\n' % (int(ii['id'])))
            file.write("name:" + ii['name'] + '\n')
            #file.write('description:' + ii['description'] + '\n')
            #file.write('slug:' + ii['slug'] + '\n')
            file.write('nickname:' + ii['User']['nickname'] + '\n')
            #file.write('bio:' + ii['User']['bio'] + '\n')
            file.write('-----------------------------' + '\n')