import requests
import re
from bs4 import BeautifulSoup
session = requests.session()
# 先获取到token值
def start_request():
headers = {
'Host': 'beian.cndns.com',
'User-Agent': '''Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36''',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
}
r = session.get(url="https://beian.cndns.com/",headers=headers)
token_url = get_token_url(r.text)
token = get_token_code(token_url)
if token == None:
print("请求token失败")
else :
print("请求token成功")
print("token内容为:"+token)
get_icp_msg(token)
pass
# 获取到返回token值的url地址
def get_token_url(text):
token_url = re.search("[a-z 0-9]{32}", text, flags=0).group()
# 拼接成完整的请求地址
token_url = "https://beian.cndns.com/token-cndns-icp-"+token_url+".php"
print("拼接完成的字符串为:"+token_url)
return token_url
# 获取到具体的token代码
def get_token_code(url):
token_url_headers = {
'Host': 'beian.cndns.com',
'Origin': 'https://beian.cndns.com',
'Referer': 'https://beian.cndns.com/',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
'Accept':'*/*'
}
r = session.post(url,headers = token_url_headers)
if r.status_code == 403:
print("没有访问权限.访问过快.你可以尝试更换ip")
return None
elif r.status_code == 200:
# 获取到token数据
token = r.text
return token
else:
print("异常请求码:"+r.status_code)
return None
# 汇总信息请求
def get_icp_msg(token):
icp_headers = {
'Host': 'beian.cndns.com',
'Origin': 'https://beian.cndns.com',
'Referer': 'https://beian.cndns.com/',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
'Accept':'*/*'
}
form_data = {
# 这里修改域名地址就可以了.注意是主域名
"queryCondition": 'baidu.com',
"queryConditionType": "1",
"act": "querybeianstatus",
"mulit":"1",
"token":token
}
r = session.post("https://beian.cndns.com/query_beian2.php",headers=icp_headers,data=form_data)
# 这里返回的就是相应返回html信息了.
# 你自己解析一下就好了
# 有乱码的话自己处理一下
print(r.text)
start_request()
# <table width="918" border="0" cellspacing="1" cellpadding="3" style="background:#ccc; line-height:25px; margin:10px auto; font-size:12px;">
# <tr style="background-color: #D4E6F7;">
# <td><b>主办单位名称</b></td>
# <td width="11%"><b>主办单位性质</b></td>
# <td width="15%"><b>网站备案/许可证号</b></td>
# <td><b>网站名称</b></td>
# <td><b>网站首页网址</b></td>
# <td width="9%"><b>审核时间</b></td>
# <td width="8%"><b>网站负责人</b></td>
# </tr>
# <tr>
# <td align="center" bgcolor="#FFFFFF" nowrap="nowrap">
# <div id="kind">李彦宏</div>
# </td>
# <td align="center" bgcolor="#FFFFFF" nowrap="nowrap">
# <div id="kind">个人</div>
# </td>
# <td align="center" bgcolor="#FFFFFF" nowrap="nowrap">
# 浙ICP备170131281号-4
# </td>
# <td align="center" bgcolor="#FFFFFF">
# 自我学习
# </td>
# <td align="center" bgcolor="#FFFFFF">
# <div id="home_url"><div><em><a href="http://www.baidu.com" target="_blank">www.baidu.com</a></em></div>
# </div>
# </td>
# <td align="center" bgcolor="#FFFFFF" nowrap="nowrap">
# <div id="pass_time">2018-06-21</div>
# </td>
# <td align="center" bgcolor="#FFFFFF" nowrap="nowrap">
# <div id="pass_time"></div>
# </td>
# </tr>
# </tbody></table>
网友评论