[Python] 纯文本查看 复制代码import datetime
import time
import requests
from lxml import etree
response = requests.get('https://data.rmtc.org.cn/gis/listtype0M.html')
html_parse = etree.HTML(response.text)
lis = html_parse.xpath('//*[@class="datali"]')
datas = []
for li in lis:
hr_url = li.xpath('.//div[@class="divname"]/a/@href')[0]
hr_name = (li.xpath('.//div[@class="divname"]/a/text()')[0].replace('\r', '')
.replace('\n', '')
.replace('\t', '').strip())
city = hr_name.split('(')[0]
time.sleep(0.2)
response = requests.get(f'https://data.rmtc.org.cn/gis/{hr_url}')
html_children_parse = etree.HTML(response.text)
children_lis = html_children_parse.xpath('//*[@class="datali"]')
for cl in children_lis:
hr_children_name = (cl.xpath('.//div[@class="divname"]/text()')[0].replace('\r', '')
.replace('\n', '')
.replace('\t', '').strip())
hr_children_val = cl.xpath('.//div[@class="divval"]/span[@class="label"]/text()')[0]
hr_children_time = cl.xpath('.//div[@class="divval"]/span[@class="showtime"]/text()')[0]
datas.append({'province': city.strip(), 'city': hr_children_name, 'val': hr_children_val, 'check_time': hr_children_time})
time.sleep(0.2)
with open(f'data_{datetime.datetime.now().strftime("%Y%m%d")}.txt', 'wt', encoding='UTF-8') as f:
for d in datas:
f.write(f'城市:{d["province"]}-{d["city"]},辐射值:{d["val"]},检测时间:{d["check_time"]}\r\n')