赞
踩
Python 获取综合信息(地震信息、天气、气象新闻、热点新闻、小时降雨量前30、气象预警)介绍
一、背景介绍
随着互联网技术的发展,大量的数据和信息成为了人们日常生活中不可或缺的一部分。其中,关于地震、天气、气象新闻、热点新闻、小时降雨量以及气象预警等综合信息,对于人们的日常生活和决策都有着重要的影响。因此,使用Python编写一个能够自动获取这些综合信息的程序,无疑能够大大提高人们的生活质量和效率。
二、功能介绍
三、技术实现
为了实现上述功能,我们需要使用到以下技术:
requests
库,用于发送HTTP请求,获取数据。json
库,用于解析从API返回的JSON格式数据。schedule
库,用于设置定时任务,自动获取和更新数据。pushbullet
库,用于将获取的信息推送给用户。- import requests
- from fake_useragent import UserAgent
- from selenium import webdriver
- from lxml import etree
- import json
- import time
- import datetime
- import pymysql
- from apscheduler.schedulers.blocking import BlockingScheduler
- from apscheduler.events import EVENT_JOB_EXECUTED, EVENT_JOB_ERROR
- import logging
-
-
-
- #1.天气类
- class Weather:
- def get_urls(self):
- filename='F://weather//province.json'
- f_obj=open(filename,'rb')
- urls=json.load(f_obj)
- return urls
-
- def weather_info(self,url_obj):
- url=url_obj['url']
- type=url_obj['type']
- value=url_obj['value']
- pname=url_obj['pname']
- data=[]
- response=requests.get(url=url, headers={'User-Agent': UserAgent().random})
- response.encoding = 'utf-8'
- html=response.text
- if html.find("<"+pname+" dn=\"day\">")>-1:
- result=html.replace("<"+pname+" dn=\"day\">","").replace("</"+pname+">","").replace("<city ","").replace(" an","")
- else:
- result = html.replace("<" + pname + " dn=\"nay\">", "").replace("</" + pname + ">", "").replace("<city ","").replace(" an", "")
- weather_datas = result.split("/>")
- db = DataBaseLink(host="127.0.0.1", port=6688, user='root', password='admin', db='weather')
- db_result=db.select_weather(pname)
- if db_result[0][0] == 0:
- for weather_data in weather_datas:
- if weather_data.find("stateDetailed")>-1:
- cityData = weather_data.split(" ")
- if type=="0":
- province = cityData[0].replace("quName=", "").replace("\"", "").replace("\n", "").replace("\r", "")
- state1 = cityData[3].replace("state1=", "").replace("\"", "")
- state2 = cityData[4].replace("state2=", "").replace("\"", "")
- stateDetailed = cityData[5].replace("stateDetailed=", "").replace("\"", "")
- windState = cityData[8].replace("windState=", "").replace("\"", "")
- else:
- province = cityData[2].replace("cityname=", "").replace("\"", "")
- state1 = cityData[6].replace("state1=", "").replace("\"", "")
- state2 = cityData[7].replace("state2=", "").replace("\"", "")
- stateDetailed = cityData[8].replace("stateDetailed=", "").replace("\"", "")
- windState = cityData[12].replace("windState=", "").replace("\"", "")
-
- weather_info={"province": province, "pname": pname, "type": type, "state1": state1, "state2": state2,
- "stateDetailed": stateDetailed, "windState": windState}
- if weather_info not in data:
- data.append(weather_info)
- db.close_db()
- return data
- # 插入天气数据
- def weather_insert_db(self,weather_datas):
- db = DataBaseLink(host="127.0.0.1", port=6688, user='root', password='admin', db='weather')
- for weather_data in weather_datas:
- province=weather_data['province']
- pname=weather_data['pname']
- type=weather_data['type']
- state1=weather_data['state1']
- state2=weather_data['state2']
- stateDetailed=weather_data['stateDetailed']
- windState=weather_data['windState']
- db.insert_weather(province, pname, type, state1, state2, stateDetailed, windState)
- db.commit()
- db.close_db()
注:代码版权归原作者所有,本文仅是分享原理,仅供学习和参考
四、总结
通过Python编写一个综合信息获取程序,可以帮助用户实时了解地震、天气、气象新闻、热点新闻、小时降雨量以及气象预警等关键信息,为用户的日常生活和决策提供有力的支持。同时,这也体现了Python在数据处理和信息获取方面的强大能力。
获取全部代码:https://download.csdn.net/download/aszhangwendi/88891948
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。