首页 / 爬虫 / Python爬虫获取最近七天天气预报信息
Python爬虫获取最近七天天气预报信息
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python爬虫获取最近七天天气预报信息,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2964字,纯文字阅读大概需要5分钟。
内容图文
主要用到python的requests库和BeatifulSoup库,代码如下:
#encoding:utf-8
import requests
import psycopg2
import datetime
import re
from bs4 import BeautifulSoup
from apscheduler.schedulers.background import BackgroundScheduler
#获取最近七天天气预报方法
def weatherPrediction():
cityid = "57494"
urls = ["https://tianqi.2345.com/today-"+cityid+".htm",
"https://tianqi.2345.com/tomorrow-"+cityid+".htm",
"https://tianqi.2345.com/third-"+cityid+".htm",
"https://tianqi.2345.com/fourth-"+cityid+".htm",
"https://tianqi.2345.com/fifth-"+cityid+".htm",
"https://tianqi.2345.com/sixth-"+cityid+".htm",
"https://tianqi.2345.com/seventh-"+cityid+".htm"]
for url in urls:
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
nowDate = datetime.datetime.now() # 当前日期
weatherDate = ''
if "today" in url:
weatherDate = nowDate.strftime('%Y-%m-%d')
elif "tomorrow" in url:
weatherDate = (nowDate + datetime.timedelta(days=1)).strftime('%Y-%m-%d') # 明天
elif "third" in url:
weatherDate = (nowDate + datetime.timedelta(days=2)).strftime('%Y-%m-%d') # 第三天
elif "fourth" in url:
weatherDate = (nowDate + datetime.timedelta(days=3)).strftime('%Y-%m-%d') # 第四天
elif "fifth" in url:
weatherDate = (nowDate + datetime.timedelta(days=4)).strftime('%Y-%m-%d') # 第五天
elif "sixth" in url:
weatherDate = (nowDate + datetime.timedelta(days=5)).strftime('%Y-%m-%d') # 第六天
elif "seventh" in url:
weatherDate = (nowDate + datetime.timedelta(days=6)).strftime('%Y-%m-%d') # 第七天
# -----------------------------------------天气预报基本信息开始-----------------------------------------
tianqi_list = soup.find_all("span", class_="phrase")
day_tianqi = tianqi_list[0].getText()
night_tianqi = tianqi_list[1].getText() # 天气状况
tianqi = ''
if day_tianqi == night_tianqi:
tianqi = day_tianqi
else:
tianqi = day_tianqi + "~" + night_tianqi
temp_list = soup.find_all("span", class_="temperature")
day_temp = temp_list[0].getText()
night_temp = temp_list[1].getText()
max_temp = re.findall("\d+", day_temp)[0] # 最高温
min_temp = re.findall("\d+", night_temp)[0] # 最低温
# print(weatherDate +" 天气:" + tianqi +"," + " 最高温:" + max_temp + " , " + "最低温:"+min_temp)
# -----------------------------------------天气预报基本信息结束-----------------------------------------
# -----------------------------------------天气预报额外信息开始-----------------------------------------
other_list = soup.find_all("ul", class_="parameter")[0].contents
for other in other_list:
print(other.b.getText() + " : " + other.i.getText())
# -----------------------------------------天气预报额外信息结束-----------------------------------------
# -----------------------------------------每天定时更新天气预报信息-----------------------------------------
scherduler = BackgroundScheduler()
scherduler.add_job(weatherPrediction,'interval',minutes=2)
scherduler.start()
# ----------------------------------------爬虫获取失败的提醒机制,短信/邮件,以及切换api获取天气预报信息-----------------------------------------
内容总结
以上是互联网集市为您收集整理的Python爬虫获取最近七天天气预报信息全部内容,希望文章能够帮你解决Python爬虫获取最近七天天气预报信息所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。