菜鸡源码,专注精品下载!
当前位置:首页 > 建站教程 > 建站知识

使用Python获取工信部网站备案信息

发布时间:2024-01-05  栏目:建站知识   浏览:   分类:python教程 备案查询代码

要查询工信部网站备案信息,可以使用Python的requests库和BeautifulSoup库。首先,需要安装这两个库: ```bash pip install requests pip install beautifulsoup4 ``` 然后,可以使用以下代码查询备案信息: ```python import requests from bs4 import BeautifulSoup def query_icp_record(domain): url = f'http://www.beian.miit.gov.cn/SearchResult?query={domain}' response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') record = soup.find('div', class_='record') if record: return record.text.strip() else: return '未找到备案信息' domain = 'example.com' # 替换为你要查询的域名

特点

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

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

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

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

Python代码

#-*-coding:utf-8-*-importrequests,hashlib,time,base64,cv2,oswhileTrue:#提前获取要查询的对象信息,以免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}#构造AuthKeytimeStamp=int(round(time.time()*1000))authSecret='testtest'+str(timeStamp)authKey=hashlib.md5(authSecret.encode(encoding='UTF-8')).hexdigest()#获取Cookiecookie_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(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/90.0.4430.72Safari/537.36Edg/90.0.818.42'}cookie=requests.utils.dict_from_cookiejar(requests.get('https://beian.miit.gov.cn/',headers=cookie_headers).cookies)['__jsluid_s']#请求获取Tokent_url='https://hlwicpfwc.miit.gov.cn/icpproject_query/api/auth't_headers={'Host':'hlwicpfwc.miit.gov.cn','Connection':'keep-alive','sec-ch-ua':'"NotA;Brand";v="99","Chromium";v="90","MicrosoftEdge";v="90"','Accept':'*/*','DNT':'1','sec-ch-ua-mobile':'?0','User-Agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/90.0.4430.85Safari/537.36Edg/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#获取验证图像、UUIDp_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':'"NotA;Brand";v="99","Chromium";v="90","MicrosoftEdge";v="90"','Accept':'application/json,text/plain,*/*','DNT':'1','sec-ch-ua-mobile':'?0','User-Agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/90.0.4430.85Safari/537.36Edg/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']exceptKeyError:print("请重试,请求状态码:",p_request.status_code)#解码图片,写入并计算图片缺口位置withopen('bigImage.jpg','wb')asf:f.write(base64.b64decode(big_image))f.close()withopen('smallImage.jpg','wb')asf: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_locmouse_length=position[0]+1os.remove('bigImage.jpg')os.remove('smallImage.jpg')#通过拼图验证,获取signcheck_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':'"NotA;Brand";v="99","Chromium";v="90","MicrosoftEdge";v="90"','DNT':'1','sec-ch-ua-mobile':'?0','User-Agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/90.0.4430.72Safari/537.36Edg/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']exceptException: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':'"NotA;Brand";v="99","Chromium";v="90","MicrosoftEdge";v="90"','DNT':'1','sec-ch-ua-mobile':'?0','User-Agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/90.0.4430.72Safari/537.36Edg/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("域名具体信息如下:")foriinrange(1,page_total+1):forkinrange(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']ifnotbool(domain_content_approved):domain_content_approved="无"exceptKeyError: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}ifinfo_data_page['pageNum']>page_total:print("查询完毕",'n')breakelse: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')


相关专题
评论
建站知识
建站知识
使用技巧
调试安装
运营推广