对天气数据进行抓取与分析
1、对天气数据进行抓取
import requests
from bs4 import BeautifulSoup
import pandas as pd
def getAQI(url):
headers={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36'
}
response=requests.get(url,headers=headers)
soup=BeautifulSoup(response.text,'lxml')
table=soup.find('table')
df=pd.read_html(table.prettify(),header=0)
return df[0]
year=2018
cities=['beijing','shanghai']#,'shenzhen']
for city in cities:
dfs=[]
filename=city+'-'+str(year)+'-AQI.csv'
for month in range(1,3):
url='http://www.tianqihoubao.com/aqi/'+city+'-'+str(year)+'%02d'%month+'.html'
data=getAQI(url)
dfs.append(data)
#DataF