Python_json_api 循环爬取数据

import time
import threading
from dbutils.pooled_db import PooledDB
import pymysql
from threading import Thread
import requests
import urllib3
import json
urllib3.disable_warnings()

POOL = PooledDB(
    creator=pymysql,  # 使用链接数据库的模块
    maxconnections=20,  # 连接池允许的最大连接数,0和None表示不限制连接数
    mincached=5,  # 初始化时,链接池中至少创建的链接,0表示不创建
    blocking=True,  # 连接池中如果没有可用连接后,是否阻塞等待。True,等待;False,不等待然后报错
    ping=0,  # ping MySQL服务端,检查是否服务可用。# 如:0 = None = never, 1 = default = whenever it is requested, 2 = when a cursor is created, 4 = when a query is executed, 7 = always

    host='home.vimll.com',
    port=xxxx,
    user='xxxx',
    password='xxxxx',
    database='flask',
    charset='utf8'
)

# 定义一个MyThread.py线程类
class MyThread(threading.Thread):
    def __init__(self, func, args=()):
        super(MyThread, self).__init__()
        self.func = func
        self.args = args

    def run(self):
        # time.sleep(2)
        self.result = self.func(*self.args)

    def get_result(self):
        threading.Thread.join(self)  # 等待线程执行完毕
        try:
            return self.result
        except Exception:
            return None

def task(sql):
    # 去连接池中获取一个连接
    conn = POOL.connection()
    cursor = conn.cursor()
    # cursor.execute('select * from `order`')
    # cursor.execute('select sleep(3)')
    cursor.execute(sql)
    result = cursor.fetchall()
    id_list = []
    for i in result:
        id_list.append(i[0])
    cursor.close()
    conn.close()   # 将连接放会到连接池
    return id_list

def req(x,y,z):
    #             # url = f'http://39.98.123.211:8510/admin/product/get/category1/{i}'
    url = f'http://39.98.123.211:8510/admin/product/attrInfoList/{
        x}/{y}/{z}'

    # 请求头部信息
    headers = {
        # 'Content-Type': 'application/json',
        # 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1',
        # 'token': 'c99c2735b8db4194a937ada8084f59fc',
        'Accept': 'application/json'
    }

    # POST数据
    # data = {
    #     'version': '1.1.76',
    #     'romType': '10',
    #     'description': '上传测试',
    # }

    # file = {
    #     "file": open("D:\\bili_win-install.exe", "rb")
    # }

    # 发送POST请求
    # response = requests.post(url, headers=headers, data=data, verify=False)
    response = requests.get(url, headers=headers, verify=False)

    # 打印响应的状态码和文本内容
    # print(response.status_code)
    print(f'{x}/{y}/{z}', response.text)
    try:
        json_data = response.json()
        # print(json_data)
        jtopy=json.dumps(json_data)
        # 解析JSON数据
        parsed_data = json.loads(jtopy)
        # 提取所需数据
        for item in parsed_data['data']:
            tup = (item["id"],item["attrName"],item["categoryId"],item["categoryLevel"])
            attrValueList = item['attrValueList']
            with open('C:\\Users\\xxx\\Desktop\\attrInfoList.txt','a',encoding='utf-8') as f:
                f.write(str(tup) + "\n")
            with open('C:\\Users\\xxx\\Desktop\\attrValueList.txt','a',encoding='utf-8') as f:
                f.write(str(attrValueList) + "\n")
        # with open('C:\\Users\\xxx\\Desktop\\attrInfoList.txt','a',encoding='utf-8') as f:
        #     f.write(response.text + "\n")
        # print('响应耗时', response.elapsed.total_seconds())
    except Exception as e:
        print(e)

if __name__ == '__main__':
    # 目标URL
    for x in range(1, 18):
        sql = "SELECT * FROM `category2` WHERE category1Id = {};".format(x)
        t = MyThread(task, args=(sql,))
        t.start()
        t.join()
        id1_list = t.get_result()
        print(f'id1({x})', id1_list)
        for y in id1_list:
            sql = "SELECT * FROM `category3` WHERE category2Id = {};".format(y)
            t = MyThread(task, args=(sql,))
            t.start()
            t.join()
            id2_list = t.get_result()
            print(f'id2({y})', id2_list)
            # id2_list = [13,14,83,120,169,170,171,214,215,216,80,81,82,166,167,168,19,20,21,22,32,33,34,64,78,79,84,85,86,87,93,94,95,96,97,164,99,100,101,102,115,123,126,125,128,124,129,130,132,133,135,165,136,139,140,141,143,144,145,146,147,148,149,150,151,152,162,163,172,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,200,204,205,206,207,208,209,210,211,212,213,240,6374,6375,336,337,346,347,348,349,350,351,360,361,363,364,440,441,442,450,465,466,511,512,525,528,529,530,533,534,538,539,540,541,542,543,544,589,591,592,751,752,753,754,757,758,759,760,761,762,763,764,779,780,781,782,846,6702,6703,6704,849,855,856,860,862,863,864,865,880,884,887,888,889,890,9005,898,906,907,908,909,910,911,4602,4603,4604,918,920,922,923,929,930,936,937,938,939,942,943,945,948,952,955,956,957,958,959,963,964,965,966,1021,1022,1025,1029,1030,1031,1038,1040,1044,1056,1058,1077,1085,8579,8580,1124,1126,1127,1154,1376,1377,1378,1163,1241,1167,1176,1186,1187,1240,1253,1254,1256,1257,8931,1328,1329,1330,1331,1299,1300,1301,1321,1322,1323,1324,1325,8241,8242,1362,1379,1380,1381,1382,1383,1384,1399,7809,1449,1450,1446,1459,1460,1461,1462,1463,1562,1563,1564,1600,1765,1766,1767,1658,1659,1665,1666,1667,1668,1669,1671,1672,1673,1674,1676,1677,1679,1680,1681,1682,1686,1689,1691,1692,1693,1694,1695,3186,8986,1701,1704,1705,10854,1827,9719,1829,1831,1832,1833,1834,1837,1843,1845,1846,1847,1848,1849,1851,1896,1897,1898,1899,1900,1901,1902,1903,1904,1921,9212,9213,1925,9475,9476,8499,7312,7313,2010,2011,2012,2034,2105,2115,2116,2117,2145,2199,2200,2201,2202,2203,2204,2205,2208,2209,2262,2264,2265,2266,2267,2268,2269,2270,2271,2339,2340,2341,2342,2345,2346,2511,2512,2513,2517,2518,2519,2520,2521,2526,2527,2528,2529,2530,2531,2532,2533,2537,2538,2540,2541,2543,2544,2545,2546,2547,2550,2557,5612,5613,2645,2646,2681,2682,2683,2684,2687,2688,2689,2690,2691,2692,2693,2694,2695,2696,2710,2711,2714,2715,2716,2717,2718,2719,2750,2846,2847,2877,2878,2879,2880,2881,2884,2888,2889,2901,2902,2907,2908,2909,3330,3331,2967,2970,2971,2972,2973,2989,2990,3010,3011,10076,10077,3015,3016,3017,3096,3019,3089,3119,3120,3121,3122,3138,3139,3140,3328,3329,3150,3152,3153,3154,3155,3156,3160,3161,3169,3174,3177,3261,3180,3214,10872,3250,3251,3252,3253,3254,3260,3262,3263,3264,3265,3270,3271,3296,3297,3298,3299,3301,3302,3303,3318,3319,3413,3361,3362,3363,3388,3389,3424,3425,3430,3431,3432,9941,9942,4161,4162,4163,4164,3543,3544,3545,3546,3547,3549,3550,3556,3574,3575,3576,3577,3578,3592,3593,3594,3599,3600,3601,3603,3605,3650,3651,3653,3655,3668,3671,3709,3742,3756,3757,3758,3765,3766,3768,3769,3770,3771,3778,3779,3780,3781,3786,3787,3791,3832,3833,3834,3835,3852,3853,3866,3867,3868,3869,3870,3916,3932,3933,3949,3950,3951,3952,3953,3955,3956,3974,3975,3976,3977,3993,3994,3995,3996,3998,4152,4153,4154,4155,4156,4165,4166,4167,4168,4169,4175,4176,4177,4178,4179,4212,4213,10739,10740,10741,10742,4432,4433,4434,4435,4436,4437,4438,10394,10395,10396,4443,4448,4449,4451,4453,4455,4458,4459,4460,4464,4470,4471,4472,4473,4474,4506,4507,4508,4510,4512,4513,4514,4515,4516,4518,4519,4520,4521,4524,4525,4527,4528,4529,4530,4531,4532,4533,4534,4535,4536,4541,4542,4582,4583,4584,4585,4597,4598,4599,4600,4601,7710,7711,7712,7713,4681,4682,4709,4710,4711,4712,4762,4763,4764,4765,4766,4767,4839,4840,4841,4807,4808,4814,4815,4816,4817,5126,5127,5128,4853,4901,4903,4904,4932,10810,10811,5027,5129,5130,5143,5228,5229,5248,5249,5250,5251,5252,5253,5254,5322,10624,5332,5363,5364,5365,5367,5368,5370,5378,5379,5396,5397,5414,5461,5462,5463,5464,5465,5466,5467,5468,5469,5470,5471,5472,5473,5474,5475,5476,5477,5478,5479,5480,5481,5482,5483,5484,5495,5496,5497,5498,10393,5536,5537,5538,5539,5540,5541,5542,5543,5584,6392,6393,5622,5623,5624,5625,5626,5627,5628,5629,5631,5632,5633,5634,5635,5636,5637,5638,5639,5648,5689,10893,5746,10871,5792,6610,6611,6612,5824,5825,5826,5827,5828,5834,5835,5877,8565,8566,5930,5931,5932,5933,5934,5936,5957,5958,5959,5960,5961,5962,5964,5965,5985,5986,5987,5988,5989,5990,5991,5994,5995,5999,6000,6001,6485,6134,6135,8883,6206,6207,6208,6210,6211,6249,6250,6251,6252,6322,6330,6376,6377,6378,6450,6451,6454,6455,6456,6460,10392,7814,6827,6828,6509,6514,6515,6520,6521,6522,6523,6527,6577,6578,6579,6580,6581,6582,6583,6584,6585,6586,6587,6588,6589,6590,6591,9507,9508,9509,6618,6619,6628,6629,6630,6631,6632,6633,6634,6738,6976,6982,6985,6986,7000,9550,9551,9552,7065,7066,7067,7068,7069,7070,7071,7072,7073,7074,7075,7076,7077,7214,7215,7218,7219,7220,7221,7263,7272,7280,7281,7282,7289,7290,7291,7347,7348,7349,7350,7351,7362,7383,7442,7466,7592,7645,7646,7647,7648,7649,7650,7651,7652,7653,7654,7670,7692,7693,7755,7756,7757,7758,7764,7766,7767,7768,7775,7777,7810,7902,7930,7931,7932,7947,7948,7959,7960,7961,7962,7963,7970,7971,7975,7980,7982,7984,7985,7986,7993,7994,7995,7996,7997,7998,8000,8001,8002,8003,8004,8005,8006,8007,8512,8009,8010,8011,8012,8013,8032,8035,8036,8084,8086,8090,8091,8092,8093,8094,8095,8096,8097,8098,8099,8100,8101,8102,9129,9130,10793,10794,10795,10796,10797,10798,9135,9136,9137,8160,8161,8162,8163,8164,8169,8170,8171,8172,8173,8212,8214,8215,8216,8225,8226,8227,8222,8223,8224,8238,8239,8240,8313,8314,8315,8316,8317,8318,8319,8320,8321,8322,8323,8324,8325,8326,8327,8328,8329,8330,8331,8332,8333,8334,8335,8336,8337,8338,8339,8340,8341,8342,8343,8348,8349,8350,8351,8352,8353,8354,8355,8356,8357,8358,8359,8362,8363,8364,8365,8367,8369,8371,10802,10803,8394,8395,8398,8399,8440,8441,8451,8452,8453,8454,8455,8513,8464,8465,8466,8497,8989,8514,8531,8532,8552,8553,8568,8572,8597,8598,8599,8600,8601,8602,8603,8604,8605,8608,8611,8651,8653,8654,8659,8664,8665,8668,8669,8685,8686,8687,8688,8946,8947,9361,9362,8701,8704,8705,8706,8707,8716,8717,9500,8719,8732,8733,8734,8735,8736,8737,8738,8739,8740,8741,8802,8803,8804,8751,8752,8753,8754,8759,8760,8764,8765,8767,8768,8769,8770,8771,8772,8773,8774,8775,8781,8783,8784,8801,8799,8788,8789,8790,8791,8792,8793,8794,8800,8797,8798,8837,8838,8839,8841,8844,8845,8846,8847,8848,8849,8850,8851,8852,8853,8854,8860,8862,8863,8864,8865,9067,9068,9069,8877,8886,8887,8888,8889,8890,8891,8892,8893,8894,8895,8896,8897,8898,8899,8900,8901,8902,8903,8904,8905,8906,8907,8908,8927,8933,8934,9502,8991,9009,9010,9011,9035,9041,9042,9046,9047,9048,9113,9117,9182,9183,9219,9220,9229,9230,9259,9260,9261,9346,9347,9391,9392,9393,9399,9400,9401,9402,9406,9407,9408,9414,9415,9416,9417,9418,9425,9426,9427,9428,9442,9443,9448,9449,9459,9468,9469,9470,9471,9472,9473,9477,9493,9494,9495,9496,9503,9504,9505,9506,9510,9511,9512,9513,9514,9515,9520,9523,9526,9527,9528,9529,9530,9531,9532,9535,9536,9537,9538,9539,9540,9542,9543,9546,9547,9553,9562,9563,10389,10390,10391,9564,9580,9581,9585,9586,10388,9589,9590,9591,9595,10370,10371,9607,9608,10372,10373,9612,9626,9627,9628,9668,9669,9670,9671,9672,9673,9674,9675,9676,9677,9678,9679,9680,9681,9685,9686,9687,9688,9695,9696,9697,9698,9699,9700,9701,9704,9705,9706,9707,9708,9709,9753,9764,9765,9766,9767,9768,9774,9784,9785,9786,9787,9788,9789,9790,9791,9792,9793,9794,9795,9796,9797,9798,9799,9800,9801,9802,9803,9804,9805,9806,9807,9820,9821,9822,9823,9824,9825,9826,9827,9828,9829,9830,9831,9832,9833,10084,10085,10086,9846,9847,9893,9903,9904,9905,9906,9943,9944,9945,9993,9994,9995,9996,10003,10010,10021,10022,10042,10087,10088,10089,10090,10091,10092,10093,10094,10095,10120,10121,10122,10128,10129,10130,10131,10132,10176,10177,10192,10193,10194,10195,10196,10197,10198,10199,10203,10204,10205,10210,10211,10212,10213,10222,10223,10335,10336,10337,10338,10339,10340,10341,10342,10343,10344,10345,10349,10350,10355,10356,10357,10365,10366,10367,10368,10369,10386,10387,10400,10401,10408,10409,10410,10411,10412,10413,10414,10415,10416,10417,10418,10419,10512,10513,10514,10515,10516,10525,10537,10538,10539,10540,10541,10542,10543,10544,10545,10546,10547,10553,10554,10555,10556,10557,10558,10559,10560,10561,10562,10563,10564,10565,10566,10569,10570,10582,10589,10613,10614,10617,10618,10619,10761,10762,10626,10627,10628,10631,10635,10636,10637,10638,10639,10640,10641,10642,10643,10644,10645,10646,10648,10649,10650,10870,10699,10700,10701,10702,10812,10813,10814,10711,10712,10851,10852,10853,10753,10754,10759,10760,10764,10765,10767,10801,10817,10818,10819,10834,10835,10858,10861,10862,10866,10867,10876,10894,10917]
            for z in id2_list:
                t = threading.Thread(target=req, args=(x,y,z))
                t.start()
                t.join()