Python爬虫学习:用API爬取天气预报数据

和风天气会提供一个API接口,方便其他的开发人员或者是学生,之前做手机APP的时候就使用过,现在回头看数据爬虫的东西,发现之前的接口已经不能用了,好可惜啊。

虽然不能连接,但是展示下思路吧。

1.首先获取所有城市的ID

https://dev.heweather.com/docs/refer/city下载中国城市的csv文件,运行下面这段代码可以提取所有城市ID:(要去除文件的第一行)

import pandas as pd
df = pd.read_csv('china-city-list.csv')

for item in df['City_ID']:
    print(item)

  

2.完成了城市编码的提取后,下一步就是调用接口获取数据,代码如下:

import requests
import time
import pandas as pd
df = pd.read_csv('china-city-list.csv')
for item in df['City_ID']:
    url = 'https://free-api.heweather.net/s6/weather/now?location=' +item+ '&key=1f0ed5e276a04402bc761d764d8f7cc8'

    strhtml = requests.get(url)
    strhtml.encoding = 'utf8'
    time.sleep(1)

  

3.存储数据到MongoDB

import requests
import pymongo
import pandas as pd
# 建立链接,其中localhost是主机名,2017是端口号(默认情况下是这个参数)
client = pymongo.MongoClient('localhost', 27017)
# 新建名为'weather'的数据库
book_weather = client['weather']
# 新建名为'sheet_weather_3'的表
sheet_weather = book_weather['sheet_weather_3']
df = pd.read_csv('china-city-list.csv')
for item in df['City_ID']:
    url = 'https://free-api.heweather.net/s6/weather/now?location=' +item+ '&key=1f0ed5e276a04402bc761d764d8f7cc8'
    strhtml = requests.get(url)
    strhtml.encoding = 'utf8'
    # requests库返回的数据可以编码成JSON格式的数据
    dic = strhtml.json()
    # 写入数据
    sheet_weather.insert_one(dic)

  

MongoDB里没有数据啊。

 4.MongoDB查询数据

4.1 查找键 HeWeather6.basic.location 值为北京的数据

import pymongo
client = pymongo.MongoClient('localhost', 27017)
book_weather = client['weather']
sheet_weather = book_weather['sheet_weather_3']
for item in sheet_weather.find({'HeWeather6.basic.location':"北京"}):
    print(item)

  

4.2 查询最低气温大于5度的城市,代码如下:

import pymongo
client = pymongo.MongoClient('localhost', 27017)
book_weather = client['weather']
sheet_weather = book_weather['sheet_weather_3']
for item in sheet_weather.find():
    tmp = item['HeWeather6'][0]['now']['tmp']['min']
    # 将表中的气温数据修改为数值型
    sheet_weather.update_one({'_id':item['_id']},{'$set':{'HeWeather6.0.now.tmp':int(tmp)}})
    # 提取表中最低气温高于5摄氏度的城市
for item in sheet_weather.find({'HeWeather6.0.now.tmp':{'$gt':5}}):
    print(item['HeWeather6'][0]['basic']['city'])

  

4.3 查询三天里,天气最低温大于5度的城市,代码如下:

import pymongo
client = pymongo.MongoClient('localhost', 27017)
book_weather = client['weather']
sheet_weather = book_weather['sheet_weather_3']
for item in sheet_weather.find():
    # 因为数据需要3天的天气预报,因此要循环3次
    for i in range(3):
        tmp = item['HeWeather6'][0]['daily_forecast'][i]['tmp']['min']
        sheet_weather.update_one({'_id':item['_id']},{'$set':{'HeWeather6.0.daily_forecast.{}.tmp.min'.format(i):int(tmp)}})
for item in sheet_weather.find({'HeWeather6.0.daily_forecast.tmp.min':{'$gt':5}}):
    print(item['HeWeather6'][0]['basic']['city'])

  

猜你喜欢

转载自www.cnblogs.com/zhuozige/p/12976293.html
今日推荐