Python爬蟲獲取最近七天天氣預報資訊

2021-08-28 16:19:07 字數 3115 閱讀 4681

主要用到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 = [""+cityid+".htm",

""+cityid+".htm",

""+cityid+".htm",

""+cityid+".htm",

""+cityid+".htm",

""+cityid+".htm",

""+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獲取天氣預報資訊-----------------------------------------

sql語句查詢昨天,今天,最近七天,最近三十天資料

今天的所有資料 select from 表名 where to days 時間欄位名 to days now 昨天的所有資料 select from 表名 where to days now to days 時間欄位名 1 7天內的所有資料 select from 表名 where date sub...

sql語句查詢最近七天 三十天 資料

幾個小時內的資料 date sub now interval 5 hour 今天 select from 表名 where to days 時間欄位名 to days now 昨天 select from 表名 where to days now to days 時間欄位名 1 7天 select ...

sql語句查詢最近七天 三十天 資料

幾個小時內的資料 date sub now interval 5 hour 今天select from 表名 where to days 時間欄位名 to days now 昨天select from 表名 where to days now to days 時間欄位名 1 7天select fro...