天眼查统计数据爬取

天眼查的高级搜索可以在不购买会员的情况下,得到特点筛选条件的统计数量。因此,可以通过爬虫爬取我们需要的统计数据,并且能够细化到区县级别。

其数据请求分为两次:

一次是获取用户选择的区域,并返回对应区域的区域代码,储存在返回json的option键中。


https://capi.tianyancha.com/cloud-tempest/company/advance?_=${timestamp}

 

另一次请求时给出对应的区域代码,以及其他筛选指标,向服务器请求结果。


https://capi.tianyancha.com/cloud-tempest/company/advance?_=${timestamp}

然而,区域代码分为省、市、区县,且并不连续,需要构造循环测试其代码,整体js代码如下,使用时需要修改网站登录后请求的X-AUTH-TOKEN和X-TYCID,否则无法获取数据。总体代码如下:


async function sendProvinceRequest(area) {
    // 生成当前时间戳(毫秒级)
    const timestamp = Date.now();
    const url = `https://capi.tianyancha.com/cloud-tempest/company/advance/filter/show?_=${timestamp}`;
    
    // 定义请求头
    const headers = new Headers({
        'Accept': 'application/json, text/plain, */*',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Content-Type': 'application/json',
        'Host': 'capi.tianyancha.com',
        'Origin': 'https://www.tianyancha.com',
        'Referer': 'https://www.tianyancha.com/',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'same-site',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36',
        'X-AUTH-TOKEN': 'eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxNTg1ODI0OTQ1MCIsImlhdCI6MTc1NTUyOTYyOCwiZXhwIjoxNzU4MTIxNjI4fQ.WZ5C5Z4rKaFgP5ouFLuwoS7lJaQj0F0Yh1PlP0c0OKjjBwKc-w-1SAG7L4OH3s7o0DX1dMTRVa7a79ScOF7hfA',
        'X-TYCID': 'f1a09a207c4411f0a3454539a59efae1',
        'sec-ch-ua': '"Not)A;Brand";v="24", "Chromium";v="116"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
        'version': 'TYC-Web'
    });

    // POST 负载数据 - 动态设置area参数值
    const payload = {
        "filterJson": JSON.stringify({
            "searchMethod": {"key":"searchMethod","items":[{"value":"1"}]},
            "institutionTypeMethod": {"key":"institutionTypeMethod","items":[{"value":"1"}]},
            "incomeReportYear": {"key":"incomeReportYear","items":[{"value":"最新"}]},
            "incomeReportPeriod": {"key":"incomeReportPeriod","items":[{"value":"12"}]},
            "incomeRelation": {"key":"incomeRelation","items":[{"value":"gt"}]},
            "economicTypeMethod": {"key":"economicTypeMethod","items":[{"value":"1"}]},
            "technologyTypeNewMethod": {"key":"technologyTypeNewMethod","items":[{"value":"1"}]},
            "enterTopListMethod": {"key":"enterTopListMethod","items":[{"value":"1"}]},
            "certificateTypeMethod": {"key":"certificateTypeMethod","items":[{"value":"1"}]},
            "incomeUnit": {"key":"incomeUnit","items":[{"value":"万"}]},
            "sortType": {"key":"sortType","items":[{"value":"0"}]},
            "profitUnit": {"key":"profitUnit","items":[{"value":"万"}]},
            "profitRelation": {"key":"profitRelation","items":[{"value":"gt"}]},
            "profitReportYear": {"key":"profitReportYear","items":[{"value":"最新"}]},
            "profitReportPeriod": {"key":"profitReportPeriod","items":[{"value":"12"}]},
            "assetUnit": {"key":"assetUnit","items":[{"value":"万"}]},
            "assetRelation": {"key":"assetRelation","items":[{"value":"gt"}]},
            "assetReportYear": {"key":"assetReportYear","items":[{"value":"最新"}]},
            "assetReportPeriod": {"key":"assetReportPeriod","items":[{"value":"12"}]},
            "liabilityUnit": {"key":"liabilityUnit","items":[{"value":"万"}]},
            "liabilityRelation": {"key":"liabilityRelation","items":[{"value":"gt"}]},
            "liabilityReportYear": {"key":"liabilityReportYear","items":[{"value":"最新"}]},
            "liabilityReportPeriod": {"key":"liabilityReportPeriod","items":[{"value":"12"}]},
            "financialDataMethod": {"key":"financialDataMethod","items":[{"value":"1"}]},
            // 动态设置区域编码
            "areaCode": {"key":"areaCode","items":[{"value": area}]} // 修改这里
        })
    };

    try {
        // 发送 POST 请求
        const response = await fetch(url, {
            method: 'POST',
            headers: headers,
            body: JSON.stringify(payload)
        });

        // 检查响应状态
        if (!response.ok) {
            throw new Error(`HTTP error! Status: ${response.status}`);
        }

        // 解析并返回 JSON 响应
        const data = await response.json();
        jdata= JSON.parse(JSON.stringify(data.data.filterShow).replace(/\\/g, '').replace(/^"|"$/g, '').replace(/^[|]$/g, ''))[0].option;
        console.log('POST请求成功,响应数据:', jdata);
        return jdata;
        
    } catch (error) {
        console.error('请求失败:', error);
        throw error;
    }
}

async function sendCityRequest(area,city) {
    // 生成当前时间戳(毫秒级)
    const timestamp = Date.now();
    const url = `https://capi.tianyancha.com/cloud-tempest/company/advance/filter/show?_=${timestamp}`;
    
    // 定义请求头
    const headers = new Headers({
        'Accept': 'application/json, text/plain, */*',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Content-Type': 'application/json',
        'Host': 'capi.tianyancha.com',
        'Origin': 'https://www.tianyancha.com',
        'Referer': 'https://www.tianyancha.com/',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'same-site',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36',
        'X-AUTH-TOKEN': 'eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxNTg1ODI0OTQ1MCIsImlhdCI6MTc1NTUyOTYyOCwiZXhwIjoxNzU4MTIxNjI4fQ.WZ5C5Z4rKaFgP5ouFLuwoS7lJaQj0F0Yh1PlP0c0OKjjBwKc-w-1SAG7L4OH3s7o0DX1dMTRVa7a79ScOF7hfA',
        'X-TYCID': 'f1a09a207c4411f0a3454539a59efae1',
        'sec-ch-ua': '"Not)A;Brand";v="24", "Chromium";v="116"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
        'version': 'TYC-Web'
    });

    // POST 负载数据 - 动态设置area参数值
    const payload = {
        "filterJson": JSON.stringify({
            "searchMethod": {"key":"searchMethod","items":[{"value":"1"}]},
            "institutionTypeMethod": {"key":"institutionTypeMethod","items":[{"value":"1"}]},
            "incomeReportYear": {"key":"incomeReportYear","items":[{"value":"最新"}]},
            "incomeReportPeriod": {"key":"incomeReportPeriod","items":[{"value":"12"}]},
            "incomeRelation": {"key":"incomeRelation","items":[{"value":"gt"}]},
            "economicTypeMethod": {"key":"economicTypeMethod","items":[{"value":"1"}]},
            "technologyTypeNewMethod": {"key":"technologyTypeNewMethod","items":[{"value":"1"}]},
            "enterTopListMethod": {"key":"enterTopListMethod","items":[{"value":"1"}]},
            "certificateTypeMethod": {"key":"certificateTypeMethod","items":[{"value":"1"}]},
            "incomeUnit": {"key":"incomeUnit","items":[{"value":"万"}]},
            "sortType": {"key":"sortType","items":[{"value":"0"}]},
            "profitUnit": {"key":"profitUnit","items":[{"value":"万"}]},
            "profitRelation": {"key":"profitRelation","items":[{"value":"gt"}]},
            "profitReportYear": {"key":"profitReportYear","items":[{"value":"最新"}]},
            "profitReportPeriod": {"key":"profitReportPeriod","items":[{"value":"12"}]},
            "assetUnit": {"key":"assetUnit","items":[{"value":"万"}]},
            "assetRelation": {"key":"assetRelation","items":[{"value":"gt"}]},
            "assetReportYear": {"key":"assetReportYear","items":[{"value":"最新"}]},
            "assetReportPeriod": {"key":"assetReportPeriod","items":[{"value":"12"}]},
            "liabilityUnit": {"key":"liabilityUnit","items":[{"value":"万"}]},
            "liabilityRelation": {"key":"liabilityRelation","items":[{"value":"gt"}]},
            "liabilityReportYear": {"key":"liabilityReportYear","items":[{"value":"最新"}]},
            "liabilityReportPeriod": {"key":"liabilityReportPeriod","items":[{"value":"12"}]},
            "financialDataMethod": {"key":"financialDataMethod","items":[{"value":"1"}]},
            // 动态设置区域编码
            "areaCode": {"key":"areaCode","items":[{"value":area,"childList":[{"value":city}]}]}// 修改这里
        })
    };

    try {
        // 发送 POST 请求
        const response = await fetch(url, {
            method: 'POST',
            headers: headers,
            body: JSON.stringify(payload)
        });

        // 检查响应状态
        if (!response.ok) {
            throw new Error(`HTTP error! Status: ${response.status}`);
        }

        // 解析并返回 JSON 响应
        const data = await response.json();
        jdata= JSON.parse(JSON.stringify(data.data.filterShow).replace(/\\/g, '').replace(/^"|"$/g, '').replace(/^[|]$/g, ''))[0].option;
        console.log('POST请求成功,响应数据:', jdata);
        return jdata;
        
    } catch (error) {
        console.error('请求失败:', error);
        throw error;
    }
}

async function sendCountyRequest(area,city,county) {
    // 生成当前时间戳(毫秒级)
    const timestamp = Date.now();
    const url = `https://capi.tianyancha.com/cloud-tempest/company/advance/filter/show?_=${timestamp}`;
    
    // 定义请求头
    const headers = new Headers({
        'Accept': 'application/json, text/plain, */*',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Content-Type': 'application/json',
        'Host': 'capi.tianyancha.com',
        'Origin': 'https://www.tianyancha.com',
        'Referer': 'https://www.tianyancha.com/',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'same-site',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36',
        'X-AUTH-TOKEN': 'eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxNTg1ODI0OTQ1MCIsImlhdCI6MTc1NTUyOTYyOCwiZXhwIjoxNzU4MTIxNjI4fQ.WZ5C5Z4rKaFgP5ouFLuwoS7lJaQj0F0Yh1PlP0c0OKjjBwKc-w-1SAG7L4OH3s7o0DX1dMTRVa7a79ScOF7hfA',
        'X-TYCID': 'f1a09a207c4411f0a3454539a59efae1',
        'sec-ch-ua': '"Not)A;Brand";v="24", "Chromium";v="116"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
        'version': 'TYC-Web'
    });

    // POST 负载数据 - 动态设置area参数值
    const payload = {
        "filterJson": JSON.stringify({
            "searchMethod": {"key":"searchMethod","items":[{"value":"1"}]},
            "institutionTypeMethod": {"key":"institutionTypeMethod","items":[{"value":"1"}]},
            "incomeReportYear": {"key":"incomeReportYear","items":[{"value":"最新"}]},
            "incomeReportPeriod": {"key":"incomeReportPeriod","items":[{"value":"12"}]},
            "incomeRelation": {"key":"incomeRelation","items":[{"value":"gt"}]},
            "economicTypeMethod": {"key":"economicTypeMethod","items":[{"value":"1"}]},
            "technologyTypeNewMethod": {"key":"technologyTypeNewMethod","items":[{"value":"1"}]},
            "enterTopListMethod": {"key":"enterTopListMethod","items":[{"value":"1"}]},
            "certificateTypeMethod": {"key":"certificateTypeMethod","items":[{"value":"1"}]},
            "incomeUnit": {"key":"incomeUnit","items":[{"value":"万"}]},
            "sortType": {"key":"sortType","items":[{"value":"0"}]},
            "profitUnit": {"key":"profitUnit","items":[{"value":"万"}]},
            "profitRelation": {"key":"profitRelation","items":[{"value":"gt"}]},
            "profitReportYear": {"key":"profitReportYear","items":[{"value":"最新"}]},
            "profitReportPeriod": {"key":"profitReportPeriod","items":[{"value":"12"}]},
            "assetUnit": {"key":"assetUnit","items":[{"value":"万"}]},
            "assetRelation": {"key":"assetRelation","items":[{"value":"gt"}]},
            "assetReportYear": {"key":"assetReportYear","items":[{"value":"最新"}]},
            "assetReportPeriod": {"key":"assetReportPeriod","items":[{"value":"12"}]},
            "liabilityUnit": {"key":"liabilityUnit","items":[{"value":"万"}]},
            "liabilityRelation": {"key":"liabilityRelation","items":[{"value":"gt"}]},
            "liabilityReportYear": {"key":"liabilityReportYear","items":[{"value":"最新"}]},
            "liabilityReportPeriod": {"key":"liabilityReportPeriod","items":[{"value":"12"}]},
            "financialDataMethod": {"key":"financialDataMethod","items":[{"value":"1"}]},
            // 动态设置区域编码
            "areaCode": {"key":"areaCode","items":[{"value":area,"childList":[{"value":city,"childList":[{"value":county}]}]}]}// 修改这里
        })
    };

    try {
        // 发送 POST 请求
        const response = await fetch(url, {
            method: 'POST',
            headers: headers,
            body: JSON.stringify(payload)
        });

        // 检查响应状态
        if (!response.ok) {
            throw new Error(`HTTP error! Status: ${response.status}`);
        }

        // 解析并返回 JSON 响应
        const data = await response.json();
        jdata= JSON.parse(JSON.stringify(data.data.filterShow).replace(/\\/g, '').replace(/^"|"$/g, '').replace(/^[|]$/g, ''))[0].option;
        console.log('POST请求成功,响应数据:', jdata);
        return jdata;
        
    } catch (error) {
        console.error('请求失败:', error);
        throw error;
    }
}
async function sendZhixiashiSHZZRequest(area,city) {
        // 生成当前时间戳(毫秒级)
    const timestamp = Date.now();
    const url = `https://capi.tianyancha.com/cloud-tempest/company/advance?_=${timestamp}`;
    
    // 定义请求头
    const headers = new Headers({
        'Accept': 'application/json, text/plain, */*',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Content-Type': 'application/json',
        'Host': 'capi.tianyancha.com',
        'Origin': 'https://www.tianyancha.com',
        'Referer': 'https://www.tianyancha.com/',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'same-site',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36',
        'X-AUTH-TOKEN': 'eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxNTg1ODI0OTQ1MCIsImlhdCI6MTc1NTU2NzkzNSwiZXhwIjoxNzU4MTU5OTM1fQ.eBT8tXXyxniP4aKPZYjqmMb8odTFqnus4ekkgNhT3_z7jrnzcbrMU_v2eiEUBSgbucXP9UlSHShQjLtU1KI8Ug',
        'X-TYCID': 'ead585f01e5911f09b3ed7c5319b5f25',
        'sec-ch-ua': '"Not)A;Brand";v="24", "Chromium";v="116"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
        'version': 'TYC-Web'
    });

    // POST 负载数据 - 动态设置area参数值
    payload = {
        "filterJson": JSON.stringify({
        "searchMethod": {"key": "searchMethod", "items": [{"value": "1"}]},
        "institutionTypeMethod": {"key": "institutionTypeMethod", "items": [{"value": "1"}]},
        "incomeReportYear": {"key": "incomeReportYear", "items": [{"value": "最新"}]},
        "incomeReportPeriod": {"key": "incomeReportPeriod", "items": [{"value": "12"}]},
        "incomeRelation": {"key": "incomeRelation", "items": [{"value": "gt"}]},
        "economicTypeMethod": {"key": "economicTypeMethod", "items": [{"value": "1"}]},
        "technologyTypeNewMethod": {"key": "technologyTypeNewMethod", "items": [{"value": "1"}]},
        "enterTopListMethod": {"key": "enterTopListMethod", "items": [{"value": "1"}]},
        "certificateTypeMethod": {"key": "certificateTypeMethod", "items": [{"value": "1"}]},
        "incomeUnit": {"key": "incomeUnit", "items": [{"value": "万"}]},
        "sortType": {"key": "sortType", "items": [{"value": "0"}]},
        "profitUnit": {"key": "profitUnit", "items": [{"value": "万"}]},
        "profitRelation": {"key": "profitRelation", "items": [{"value": "gt"}]},
        "profitReportYear": {"key": "profitReportYear", "items": [{"value": "最新"}]},
        "profitReportPeriod": {"key": "profitReportPeriod", "items": [{"value": "12"}]},
        "assetUnit": {"key": "assetUnit", "items": [{"value": "万"}]},
        "assetRelation": {"key": "assetRelation", "items": [{"value": "gt"}]},
        "assetReportYear": {"key": "assetReportYear", "items": [{"value": "最新"}]},
        "assetReportPeriod": {"key": "assetReportPeriod", "items": [{"value": "12"}]},
        "liabilityUnit": {"key": "liabilityUnit", "items": [{"value": "万"}]},
        "liabilityRelation": {"key": "liabilityRelation", "items": [{"value": "gt"}]},
        "liabilityReportYear": {"key": "liabilityReportYear", "items": [{"value": "最新"}]},
        "liabilityReportPeriod": {"key": "liabilityReportPeriod", "items": [{"value": "12"}]},
        "financialDataMethod": {"key": "financialDataMethod", "items": [{"value": "1"}]},
        "institutionType": {"key": "institutionType", "items": [{"value": "社会组织"}]},
        "establishTimeRange": {
            "key": "establishTimeRange",
            "items": [
                {"value": 0},  
                {"value": 1609430399999}  
            ]
        },
        "areaCode": {
            "key": "areaCode",
            "items": [{
                "value": area,
                "childList": [{
                    "value": city
                }]
            }]
        }
    }),
    "searchType": 2,
    "pageNum": 1,
    "pageSize": 20,
    "eventId": "i244"
}





    try {
        // 发送 POST 请求
        const response = await fetch(url, {
            method: 'POST',
            headers: headers,
            body: JSON.stringify(payload)
        });

        // 检查响应状态
        if (!response.ok) {
            throw new Error(`HTTP error! Status: ${response.status}`);
        }

        // 解析并返回 JSON 响应
        const data = await response.json();
        jdata= data.data.realTotal;
        console.log('POST请求成功,响应数据:', jdata);
        return jdata;
        
    } catch (error) {
        console.error('请求失败:', error);
        throw error;
    }
}

async function sendSHZZRequest(area,city,county) {
        // 生成当前时间戳(毫秒级)
    const timestamp = Date.now();
    const url = `https://capi.tianyancha.com/cloud-tempest/company/advance?_=${timestamp}`;
    
    // 定义请求头
    const headers = new Headers({
        'Accept': 'application/json, text/plain, */*',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Content-Type': 'application/json',
        'Host': 'capi.tianyancha.com',
        'Origin': 'https://www.tianyancha.com',
        'Referer': 'https://www.tianyancha.com/',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'same-site',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36',
        'X-AUTH-TOKEN': 'eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxNTg1ODI0OTQ1MCIsImlhdCI6MTc1NTU2NzkzNSwiZXhwIjoxNzU4MTU5OTM1fQ.eBT8tXXyxniP4aKPZYjqmMb8odTFqnus4ekkgNhT3_z7jrnzcbrMU_v2eiEUBSgbucXP9UlSHShQjLtU1KI8Ug',
        'X-TYCID': 'ead585f01e5911f09b3ed7c5319b5f25',
        'sec-ch-ua': '"Not)A;Brand";v="24", "Chromium";v="116"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
        'version': 'TYC-Web'
    });

    // POST 负载数据 - 动态设置area参数值
    payload = {
        "filterJson": JSON.stringify({
        "searchMethod": {"key": "searchMethod", "items": [{"value": "1"}]},
        "institutionTypeMethod": {"key": "institutionTypeMethod", "items": [{"value": "1"}]},
        "incomeReportYear": {"key": "incomeReportYear", "items": [{"value": "最新"}]},
        "incomeReportPeriod": {"key": "incomeReportPeriod", "items": [{"value": "12"}]},
        "incomeRelation": {"key": "incomeRelation", "items": [{"value": "gt"}]},
        "economicTypeMethod": {"key": "economicTypeMethod", "items": [{"value": "1"}]},
        "technologyTypeNewMethod": {"key": "technologyTypeNewMethod", "items": [{"value": "1"}]},
        "enterTopListMethod": {"key": "enterTopListMethod", "items": [{"value": "1"}]},
        "certificateTypeMethod": {"key": "certificateTypeMethod", "items": [{"value": "1"}]},
        "incomeUnit": {"key": "incomeUnit", "items": [{"value": "万"}]},
        "sortType": {"key": "sortType", "items": [{"value": "0"}]},
        "profitUnit": {"key": "profitUnit", "items": [{"value": "万"}]},
        "profitRelation": {"key": "profitRelation", "items": [{"value": "gt"}]},
        "profitReportYear": {"key": "profitReportYear", "items": [{"value": "最新"}]},
        "profitReportPeriod": {"key": "profitReportPeriod", "items": [{"value": "12"}]},
        "assetUnit": {"key": "assetUnit", "items": [{"value": "万"}]},
        "assetRelation": {"key": "assetRelation", "items": [{"value": "gt"}]},
        "assetReportYear": {"key": "assetReportYear", "items": [{"value": "最新"}]},
        "assetReportPeriod": {"key": "assetReportPeriod", "items": [{"value": "12"}]},
        "liabilityUnit": {"key": "liabilityUnit", "items": [{"value": "万"}]},
        "liabilityRelation": {"key": "liabilityRelation", "items": [{"value": "gt"}]},
        "liabilityReportYear": {"key": "liabilityReportYear", "items": [{"value": "最新"}]},
        "liabilityReportPeriod": {"key": "liabilityReportPeriod", "items": [{"value": "12"}]},
        "financialDataMethod": {"key": "financialDataMethod", "items": [{"value": "1"}]},
        "institutionType": {"key": "institutionType", "items": [{"value": "社会组织"}]},
        "establishTimeRange": {
            "key": "establishTimeRange",
            "items": [
                {"value": 0},  
                {"value": 1609430399999}  
            ]
        },
        "areaCode": {
            "key": "areaCode",
            "items": [{
                "value": area,
                "childList": [{
                    "value": city,
                    "childList": [{
                        "value":county
                    }]
                }]
            }]
        }
    }),
    "searchType": 2,
    "pageNum": 1,
    "pageSize": 20,
    "eventId": "i244"
}





    try {
        // 发送 POST 请求
        const response = await fetch(url, {
            method: 'POST',
            headers: headers,
            body: JSON.stringify(payload)
        });

        // 检查响应状态
        if (!response.ok) {
            throw new Error(`HTTP error! Status: ${response.status}`);
        }

        // 解析并返回 JSON 响应
        const data = await response.json();
        jdata= data.data.realTotal;
        console.log('POST请求成功,响应数据:', jdata);
        return jdata;
        
    } catch (error) {
        console.error('请求失败:', error);
        throw error;
    }
}



function generateAreaCodes(area) {
        codes=[];
        const paddedNum = area.toString().padStart(2, '0');
        codes.push(`00${paddedNum}0000V2024`);
    return codes;
}

function generateCityCodes(area,city) {
        codes=[];
        const paddedNum = area.toString().padStart(2, '0');
        const cpaddedNum = city.toString().padStart(2, '0');
        codes.push(`00${paddedNum}${cpaddedNum}00V2024`);
    return codes;
}

function generateCountyCodes(area,city,county) {
        codes=[];
        const paddedNum = area.toString().padStart(2, '0');
        const cpaddedNum = city.toString().padStart(2, '0');
        const copaddedNum = county.toString().padStart(2, '0');
        codes.push(`00${paddedNum}${cpaddedNum}${copaddedNum}V2024`);
    return codes;
}

function generateZhixiashiCodes(area,city) {
        codes=[];
        const paddedNum = area.toString().padStart(2, '0');
        const cpaddedNum = city.toString().padStart(2, '0');
        codes.push(`00${paddedNum}01${cpaddedNum}V2024`);
    return codes;
}


async function main() {
    const results = [];
    
    // 外层:循环处理区域(省/直辖市)
    for (let area = 1; area <= 99; area++) {
        try {
            // 生成区域编码并发送请求
            const areaCode = generateAreaCodes(area)[0];
            const areaName = await sendProvinceRequest(areaCode);
            
            if (areaName !== "") {
                
                // 中层:循环处理城市
                for (let city = 1; city <= 99; city++) {
                    try {
                        // 生成城市编码并发送请求
                        const cityCode = generateCityCodes(area, city)[0];
                        const cityName = await sendCityRequest(areaCode, cityCode);
                        
                        if(areaName === "北京市" || areaName === "天津市" || areaName === "上海市" || areaName === "重庆市"){
                    
                    const cityCode = generateZhixiashiCodes(area, city)[0];
                    const cityName = await sendCityRequest(areaCode, cityCode);
                    const shzznum=  await sendZhixiashiSHZZRequest(areaCode, cityCode);
                    results.push({
                                            '区域编码': areaCode,
                                            '地区名称': areaName,
                                            '城市编码': cityCode,
                                            '城市名称': cityName,
                                            '社会组织数量': shzznum
                                        });
                }
                        else if (cityName !== "" ) {
                            // 内层:循环处理区县
                            results.push({
                                            '区域编码': areaCode,
                                            '地区名称': areaName,
                                            '城市编码': cityCode,
                                            '城市名称': cityName
                                        });
                            for (let county = 1; county <= 99; county++) {
                                try {
                                    // 生成区县编码并发送请求
                                    const countyCode = generateCountyCodes(area, city, county)[0];
                                    const countyName = await sendCountyRequest(areaCode, cityCode, countyCode);
                                    
                                    if (countyName !== "") {
                                        // 保存所有层级的数据
                                        const shzznum=  await sendSHZZRequest(areaCode, cityCode, countyCode);
                                        results.push({
                                            '区域编码': areaCode,
                                            '地区名称': areaName,
                                            '城市编码': cityCode,
                                            '城市名称': cityName,
                                            '区县编码': countyCode,
                                            '区县名称': countyName,
                                            '社会组织数量': shzznum
                                        });
                                    }
                                } catch (countyError) {
                                    console.error(`区县请求失败 [${area}-${city}-${county}]:`, countyError);
                                }
                            }
                        }
                    
                    } catch (cityError) {
                        console.error(`城市请求失败 [${area}-${city}]:`, cityError);
                    }
                }
            }
        } catch (areaError) {
            console.error(`区域请求失败 [${area}]:`, areaError);
        }
        
        // 进度提示
        console.log(`区域进度: ${area}/99`);
    }

    // ✅ 此时 results 中的 '地区名称' 已是字符串,console.table 可正常显示
    console.table((JSON.parse(JSON.stringify(results))));
}
// 启动执行
main().catch(console.error);
点赞

发表回复

电子邮件地址不会被公开。必填项已用 * 标注