您的位置:首页 > 其它

获取全球dns统计信息

2017-11-01 10:35 204 查看
# -*- coding:UTF-8 -*-
import requests, time
import json
from bs4 import BeautifulSoup as bp

t3 = time.time()
ths = []  # 存放线程

def get(num):
n = str(num)
page = requests.post('http://www.employees.org/~dwing/aaaa-stats/',
)  # 自定义请求头,这些请求头内容是在浏览器上看到的

t = page.text
# print(t)
soup = bp(t, 'lxml')  # 使用beautifulsoup解析xml文件,解析html时,将xml改为lxml
all_body = soup.find_all('tr')  # 查找EmailResult标签包含的所有内容,生成一个列表
for info in all_body:
if not info:continue
tds = info.find_all('td')
one_row = []
for i in tds:
if not i:continue
for infos in i:
if not infos:continue
try:
nums = infos.text  #所有的数字
if not nums:continue
nums = nums.strip().strip('\n')
# if len(nums) >= 13:continue
if len(nums) == 12:
total_checked = nums[0:5]
with_a_record = nums[5:]
one_row.append(total_checked)
one_row.append(with_a_record)
else:
one_row.append(nums)
# print(nums,'aaa')
except:
one_row.append(infos)
# print(infos, 'lll') # 百分数

if '\n' in one_row:
one_row.remove('\n')
if 'diffs' in one_row:
one_row.remove('diffs')
if '(large run)' in one_row:
continue
else:
if one_row:
if len(one_row) == 16:
all_data = {}
all_data['date'] = one_row[0]
all_data['total_checked'] = one_row[1]
all_data['with_A_records_count'] = one_row[2]
all_data['with_A_records_rate'] = one_row[3]
all_data['with_AAAA_records_count'] = one_row[4]
all_data['with_AAAA_records_rate'] = one_row[5]
all_data['AAAA_with_IPv4-mapped_count'] = one_row[6]
all_data['AAAA_with_IPv4-mapped_rate'] = one_row[7]
all_data['AAAA_with_loopback_count'] = one_row[8]
all_data['AAAA_with_loopback_rate'] = one_row[9]
all_data['valid_AAAA_records_count'] = one_row[10]
all_data['valid_AAAA_records_rate'] = one_row[11]
all_data['IPv6_connection_ok_count'] = one_row[12]
all_data['IPv6_connection_ok_rate'] = one_row[13]
all_data['IPv6_connection_failed_count'] = one_row[14]
all_data['IPv6_connection_failed_rate'] = one_row[15]
# print(one_row)
ddd = json.dumps(all_data, indent=2,
ensure_ascii=False)  # ensure_ascii=False :防止将文字转成unicoe

with open('dns_status.txt', 'a+') as f:
f.write(ddd)
# print(i.text,'lll')

get(1)

t4 = time.time()
tt = t4 - t3
print(tt)


结果见github
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: