Python爬取天气数据&可视化的实现

本文介绍了使用Python爬取中国天气网南昌天气数据的过程,详细讲解了数据来源、爬虫代码实现,并展示了如何对获取的数据进行分析及可视化。通过numpy、pandas和matplotlib等库,成功实现了气温数据的爬取和图表展示。
摘要由CSDN通过智能技术生成

文章目录

前言

Python爬虫爬取天气数据+可视化的简单实现

我贩卖日落,你像神明一样慷慨地将光洒向我,从此点亮了人间

数据来源

数据主要选取了中国天气网中南昌的天气数据进行爬取
在这里插入图片描述
由于改页面的数据通过JSON数据包的格式进行传输,首先找到了捕获页面加载的数据包
在这里插入图片描述
找到数据接口后接下来编写代码对气温数据进行爬取

爬虫代码

#encoding=utf-8
import csv
import time
import re
import os
import requests
import json
import numpy as np
import matplotlib.pyplot as plt


url='http://d1.weather.com.cn/calendar_new/2020/101240101_'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36',
    'Referer':'http://www.weather.com.cn/',
}
filedir='./json/'
field=['alins', 'als', 'blue', 'c1', 'c2', 'cla', 'date', 'des', 'fe', 'hgl', 'hmax', 'hmin', 'hol', 'insuit', 'jq', 'max', 'maxobs', 'min', 'minobs', 'nl', 'nlyf', 'r', 'rainobs', 'suit', 't1', 't1t', 't2', 't3', 't3t', 'time', 'today', 'update', 'w1', 'wd1', 'winter', 'wk', 'wor', 'ws1', 'yl']

def getJsonData(url,headers,month):
    # print(i)
    t=time.time()
    date='2020'+'{:02}'.format(month)
    suffix='.html?_='
    rubbing=int(round(t * 1000))
    url=url+date+suffix+str(rubbing)
    response = requests.get(url, headers=headers)
    response.encoding="utf-8"
    json_code=response.text.replace('var fc40 = ','')
    json_page &#
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值