文章目录
前言
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 &#