爬取生态环境中心每日核辐射检测

查看 99|回复 8
作者:baronm   
最近因为日本核污染水排海事件,导致咱依赖大海的相关生态遭到破坏,为了知道日本的核污染水啥时候波及到咱这,特意用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')

核辐射, 日本

范俊恺   

- -  这个厉害了。
啊哈哈。。。。就是不知道怎么咋用
O2H2O   

谢谢分享,学习了!
baronm
OP
  


范俊恺 发表于 2023-9-2 20:09
- -  这个厉害了。
啊哈哈。。。。就是不知道怎么咋用

哈哈哈,就是运行起来后,会在当前目录下生成一个文本,文本中的数据就是每日监测的辐射值了
lzaiz24   

啊不错的,学习了
TheSSS   

感谢分享!!可惜没我家乡的城市
baronm
OP
  


TheSSS 发表于 2023-9-4 18:43
感谢分享!!可惜没我家乡的城市

  目前官方没有放开所有城市,都是一些代表性的
Stephanie36   

谢谢分享啦
xlc0210   

感谢分享,学习了
您需要登录后才可以回帖 登录 | 立即注册

返回顶部