【Python】爬取天气预报数据。登录中国天气网http://www.weather.com.cn,查询某个城市7天或15天天气预报,提取网页内容中信息存储到CSV文件中,同时用图片展示

本文介绍了使用Python的库如BeautifulSoup、requests和pandas,从weather.com.cn网站抓取南昌、杭州和上海三个城市的近6天天气数据,然后通过matplotlib进行数据可视化。
摘要由CSDN通过智能技术生成
import bs4
import matplotlib
import matplotlib.pyplot as plt
import requests as req
import re
import pandas as pd
import time

ls=['101240101','101210101','101020100']
City=['南昌','杭州','上海']
WEATHER=['南昌天气.csv','杭州天气.csv','上海天气.csv']
condition=['南昌近6天天气情况','杭州近6天天气情况','上海近6天天气情况']
url1='http://www.weather.com.cn/weather/'
url2='.shtml'
header={'User-Agent':'Mozilla/5.0(Windows NT 10.0;Win64;x64)'
   'AppleWebKit/537.36(KHTML,like Gecko)'
   'Chrome/79.0.3945.130 Safari/537.36 OPR/66.0.3515.115'}
matplotlib.rcParams['font.family']='SimHei'
for z in range(3):
   url=url1+ls[z]+url2
   r=req.get(url,headers=header)
   print(r.status_code)
   r.encoding='utf-8'
   soup=bs4.BeautifulSoup(r.text,'html.parser')
   Date=soup.find_all('li')#date
   date=[]
   for Date1 in Date:
      date1=Date1.find_all('h1')
      for a in date1:
         date.append(a.text)
   del(date[0])
   Max=soup.find_all('p','tem')#max
   max=[]
   min=[]
   for Max1 in Max:
      max1=Max1.find_all('span')
      for b in max1:
         max.append(b.text)
   for Min1 in Max:#min
      min1=Min1.find_all('i')
      for c in min1:
         min.append(c.text)
   del(min[0])
   Weather=re.compile('<p title="(.*?)" class="wea"')#weather
   pt=re.findall(Weather,r.text)
   weather=[]
   for e in pt:
      weather.append(e)
   del(weather[0])
   Wind=soup.find_all('p','win')
   wind=[]
   for Wind1 in Wind:
      wind1=Wind1.find_all('i')
      for i in wind1:
         wind.append(i.text)
   del(wind[0])
   print(City[z])
   print(weather)
   print(date)
   print(max)
   print(min)
   print(wind)
   time.sleep(5)

   fr1={'日期':date,'天气':weather,'风力':wind,'最高温':max,"最低温":min}
   print(fr1)
   Forecastweather=pd.DataFrame(fr1)
   print(Forecastweather)
   Forecastweather.to_csv(WEATHER[z],index=False,encoding='utf_8_sig')

   temperature=['最高温','最低温']
   MAX=[]
   MIN=[]
   x=date
   for g in max:
      MAX.append(eval(g.replace('℃',' ')))
   for h in min:
      MIN.append(eval(h.replace('℃',' ')))
   y1=MAX
   y2=MIN
   plt.figure(z+1,figsize=(6,4))
   plt.plot(x,y1,x,y2)
   plt.figtext(0.52,0.9,condition[z],ha='center',size=20)
   legend=plt.legend(temperature,loc=(0.78,0.80),labelspacing=0.1)
plt.show()

  • 12
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
爬取http://www.826226.com/lstq/110100_2020_1/页的天气数据,可以参考以下步骤: 1. 导入必要的库,如requests、BeautifulSoup等。 2. 发送HTTP请求,获取页的HTML源代码。 3. 使用BeautifulSoup库解析HTML源代码,获取所需的数据。 4. 对获取到的数据进行处理和存储。 以下是一个简单的Python代码示例,用于从http://www.826226.com/lstq/110100_2020_1/页上获取北京市今到7天气预报: ```python import requests from bs4 import BeautifulSoup # 请求头,模拟浏览器请求 headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} # 获取HTML源代码 url = 'http://www.826226.com/lstq/110100_2020_1/' response = requests.get(url, headers=headers) html = response.content.decode('utf-8') # 解析HTML源代码,获取所需的数据 soup = BeautifulSoup(html, 'html.parser') data_list = soup.select('div[class="weather6"] ul')[0].find_all('li') for data in data_list: date = data.select('h1')[0].text weather = data.select('p[class="wea"]')[0].text temperature = data.select('p[class="tem"]')[0].text print(date, weather, temperature) ``` 该代码会输出形如下面的结果: ``` 今阵雨 22℃ / 13℃ 明多云 19℃ / 10℃ 后多云 18℃ / 9℃ 周五 周五晴 19℃ / 9℃ 周六 周六多云 17℃ / 10℃ 周日 周日多云 20℃ / 10℃ 周一 周一多云 21℃ / 12℃ ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值