python教程

Python查询工信部网站备案信息

我的站长站 2021-11-01 人阅读

特点

✅ 通过 https://beian.miit.gov.cn/ 查询信息,确保与管局实际信息一致;

✅ 支持自动完成图片拖动验证,存在极低的失败率

✅ 支持循环翻页查询,获取企业名下的所有备案信息

✅ 查询完毕后按任意键继续查询

Python代码

# -*- coding: utf-8 -*-
import requests,hashlib,time,base64,cv2,os
while True:
    #提前获取要查询的对象信息,以免Token失效(Token有效时间为3分钟)
    print("项目地址:https://github.com/wongzeon/ICP-Checkern")
    print("版本:V1.1 可用测试:2021-7-27n")
    print("以企业名称查询的,需要输入企业全称n")
    print("以域名查询的,请不要输入“http/www”等域名外的字符n")
    info = input("请输入要查询的备案信息,可以为公司名或域名:")
    info_data = {
        'pageNum':'',
        'pageSize':'',
        'unitName':info
    }
    #构造AuthKey
    timeStamp = int(round(time.time()*1000))
    authSecret = 'testtest' + str(timeStamp)
    authKey = hashlib.md5(authSecret.encode(encoding='UTF-8')).hexdigest()
    #获取Cookie
    cookie_headers = {
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.72 Safari/537.36 Edg/90.0.818.42'
    }
    cookie = requests.utils.dict_from_cookiejar(requests.get('https://beian.miit.gov.cn/',headers=cookie_headers).cookies)['__jsluid_s']
    #请求获取Token
    t_url = 'https://hlwicpfwc.miit.gov.cn/icpproject_query/api/auth'
    t_headers = {
        'Host': 'hlwicpfwc.miit.gov.cn',
        'Connection': 'keep-alive',
        'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="90", "Microsoft Edge";v="90"',
        'Accept': '*/*',
        'DNT': '1',
        'sec-ch-ua-mobile': '?0',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36 Edg/90.0.818.46',
        'Origin': 'https://beian.miit.gov.cn',
        'Sec-Fetch-Site': 'same-site',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Dest': 'empty',
        'Referer': 'https://beian.miit.gov.cn/',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'Cookie': '__jsluid_s=' + cookie
    }
    data = {
        'authKey': authKey,
        'timeStamp': timeStamp
    }
    t_response = requests.post(t_url,data=data,headers=t_headers)
    try:
        get_token = t_response.json()['params']['bussiness']
    except:
        print('n'"请求被禁止,请稍后或更换头部与IP后再试,状态码:",t_response.status_code)
        break
    #获取验证图像、UUID
    p_url = 'https://hlwicpfwc.miit.gov.cn/icpproject_query/api/image/getCheckImage'
    p_headers = {
        'Host': 'hlwicpfwc.miit.gov.cn',
        'Connection': 'keep-alive',
        'Content-Length': '0',
        'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="90", "Microsoft Edge";v="90"',
        'Accept': 'application/json, text/plain, */*',
        'DNT': '1',
        'sec-ch-ua-mobile': '?0',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36 Edg/90.0.818.46',
        'token': get_token,
        'Origin': 'https://beian.miit.gov.cn',
        'Sec-Fetch-Site': 'same-site',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Dest': 'empty',
        'Referer': 'https://beian.miit.gov.cn/',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'Cookie': '__jsluid_s=' + cookie
    }
    p_request = requests.post(p_url,data='',headers=p_headers)
    try:
        p_uuid = p_request.json()['params']['uuid']
        big_image = p_request.json()['params']['bigImage']
        small_image = p_request.json()['params']['smallImage']
    except KeyError:
        print("请重试,请求状态码:",p_request.status_code)
    #解码图片,写入并计算图片缺口位置
    with open('bigImage.jpg','wb') as f:
        f.write(base64.b64decode(big_image))
        f.close()
    with open('smallImage.jpg','wb') as f:
        f.write(base64.b64decode(small_image))
        f.close()
    background_image = cv2.imread('bigImage.jpg',cv2.COLOR_GRAY2RGB)
    fill_image = cv2.imread('smallImage.jpg',cv2.COLOR_GRAY2RGB)
    background_image_canny = cv2.Canny(background_image, 100, 200)
    fill_image_canny = cv2.Canny(fill_image, 100, 300)
    position_match = cv2.matchTemplate(background_image, fill_image, cv2.TM_CCOEFF_NORMED)
    min_val,max_val,min_loc,max_loc = cv2.minMaxLoc(position_match)
    position = max_loc
    mouse_length = position[0]+1
    os.remove('bigImage.jpg')
    os.remove('smallImage.jpg')
    #通过拼图验证,获取sign
    check_url = 'https://hlwicpfwc.miit.gov.cn/icpproject_query/api/image/checkImage'
    check_headers = {
        'Host': 'hlwicpfwc.miit.gov.cn',
        'Accept': 'application/json, text/plain, */*',
        'Connection': 'keep-alive',
        'Content-Length': '60',
        'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="90", "Microsoft Edge";v="90"',
        'DNT': '1',
        'sec-ch-ua-mobile': '?0',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.72 Safari/537.36 Edg/90.0.818.42',
        'token': get_token,
        'Content-Type': 'application/json',
        'Origin': 'https://beian.miit.gov.cn',
        'Sec-Fetch-Site': 'same-site',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Dest': 'empty',
        'Referer': 'https://beian.miit.gov.cn/',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'Cookie': '__jsluid_s=' + cookie
    }
    check_data = {
        'key':p_uuid,
        'value':mouse_length
    }
    check_request = requests.post(check_url,json=check_data,headers=check_headers)
    try:
        sign = check_request.json()['params']
    except Exception:
        print('n'"请求被禁止,请稍后或更换头部与IP后再试,状态码:",check_request.status_code)
        break
    #获取备案信息
    info_url = 'https://hlwicpfwc.miit.gov.cn/icpproject_query/api/icpAbbreviateInfo/queryByCondition'
    info_headers = {
        'Host': 'hlwicpfwc.miit.gov.cn',
        'Connection': 'keep-alive',
        'Content-Length': '78',
        'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="90", "Microsoft Edge";v="90"',
        'DNT': '1',
        'sec-ch-ua-mobile': '?0',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.72 Safari/537.36 Edg/90.0.818.42',
        'Content-Type': 'application/json',
        'Accept': 'application/json, text/plain, */*',
        'uuid': p_uuid,
        'token': get_token,
        'sign': sign,
        'Origin': 'https://beian.miit.gov.cn',
        'Sec-Fetch-Site': 'same-site',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Dest': 'empty',
        'Referer': 'https://beian.miit.gov.cn/',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'Cookie': '__jsluid_s=' + cookie
    }
    info_request = requests.post(info_url,json=info_data,headers=info_headers)
    domain_total = info_request.json()['params']['total']
    page_total = info_request.json()['params']['lastPage']
    page_size = info_request.json()['params']['pageSize']
    start_row = info_request.json()['params']['startRow']
    end_row = info_request.json()['params']['endRow']
    os.system('cls')
    print("n查询对象",info,"共有",domain_total,"个备案域名",'n')
    print("域名具体信息如下:")
    for i in range(1,page_total+1):
        for k in range(start_row,end_row+1):
            info_base = info_request.json()['params']['list'][k]
            domain_name = info_base['domain']
            domain_type = info_base['natureName']
            domain_licence = info_base['mainLicence']
            domain_web_licence = info_base['serviceLicence']
            domain_site_name = info_base['serviceName']
            domain_status = info_base['limitAccess']
            domain_approve_date = info_base['updateRecordTime']
            domain_owner = info_base['unitName']
            try:
                domain_content_approved = info_base['contentTypeName']
                if not bool(domain_content_approved):
                    domain_content_approved = "无"
            except KeyError:
                domain_content_approved = "无"
            print("n域名主办方:",domain_owner,'n')
            print("域名:",domain_name,'n')
            print("网站名称:",domain_site_name,'n')
            print("备案许可证号:",domain_licence,'n')
            print("网站备案号:",domain_web_licence,'n')
            print("域名类型:",domain_type,'n')
            print("网站前置审批项:",domain_content_approved,'n')
            print("是否限制接入:",domain_status,'n')
            print("审核通过日期:",domain_approve_date,'n')
        info_data_page = {
            'pageNum':i+1,
            'pageSize':'10',
            'unitName':info
        }
        if info_data_page['pageNum'] > page_total:
            print("查询完毕",'n')
            break
        else:
            info_request = requests.post(info_url,json=info_data_page,headers=info_headers)
            start_row = info_request.json()['params']['startRow']
            end_row = info_request.json()['params']['endRow']
            time.sleep(3)
    os.system('pause')


相关专题
icp备案
icp备案
2022-08-13 22

icp备案是网站搭建必备流程,要想网站做的好,icp备案是必不可少的.我的站长站给大家整理关于icp备案相关信息,包含icp备案教程,详细教你如何网站备案,如何查询icp...

相关推荐
  • 备案查询代码
  • ThinkPHP来交易ICP备案查询系统源码v1.1
    ThinkPHP来交易ICP备案查询系统源码v1.1

    源码介绍来交易ICP备案查询系统源码v1.1版本,基于ThinkPHP框架+PHP+MYSQL开发制作,对接工信部平台,实时查询数据,快速高效。源码安装方法1、放到网站网站根目录2、访问http://你的网址/install.php3...

    php源码 62 2年前
  • 腾讯安全中心备案信息查询API接口源码

    调用的腾讯安全中心平台数据,可以查询备案号、备案主体等等信息。API源码<?phpheader("Content-type:application/json; charset=UTF-8");function getData($x){$url = "https://cgi.urlsec.qq.com/index.php?m=check&a=check&url={$x}";$header = [&...

    php教程 93 1年前
  • 最新可用的免费网站备案查询API接口
    最新可用的免费网站备案查询API接口

    最新可用的免费网站备案查询API接口,单文件版,调用的chinaz的接口。比如说一个导航的详细页面你加上这段代码也显示出了一个网站的备案信息资料这些内容,这个备案查询API我感觉只对做导航的人有用吧...

    php源码 388 4年前
最新更新