本帖最后由 baronm 于 2023-9-1 15:07 编辑
最近因为日本核污染水排海事件,导致咱依赖大海的相关生态遭到破坏,为了知道日本的核污染水啥时候波及到咱这,特意用Python简单写了个程序,以便咱们获取每日的辐射值
[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') |