3.1 注册免费API和阅读技术文档

该网站为个人开发者提供免费的预报数据(有数据限制),注册地址:http://console.heweather.com,在里面创建自己的key(密钥)。

3.2 获取API数据

import pandas as pd

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

for item in df['City_ID']:

print(item)

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

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=488e57e359e846279b277c43befa4d7c'

strhtml = requests.get(url)

strhtml.encoding = 'utf8'

time.sleep(1)

print( strhtml.text)

requests库返回的数据可以编码成JSON格式的数据,代码如下:

strhtml,json()

3.3存储数据到MongoDB

MongoDB是一个基于发布式文件存储的数据库,由C++语言编写,旨在为Web提供可扩展的高性能数据存储解决方案。

下面将数据存入MongoDB中,代码如下:

import requests

import pymongo

import pandas as pd

client = pymongo.MongoClient('localhost', 27017)

book_weather = client['weather']

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=e62719afde314cb9a130fc7cdbd7c324'

strhtml = requests.get(url)

strhtml.encoding = 'utf8'

dic = strhtml.json()

sheet_weather.insert_one(dic)

1. 建立链接

输入以下代码,其中localhost是主机名,2017是端口号(默认情况下是这个参数)

client = pymongo.MongoClient('localhost', 27017)

2. 新建名为'weather'的数据库

book_weather=client['weather']

3. 新建名为'sheet_weather_3'的表

heet_weather = book_weather['sheet_weather_3']

4. 写入数据

sheet_weather.insert_one(dic)

3.4 MongoDB 数据库查询

查询语法是sheet_weather.find(),其中sheet_weather代表weather数据库中的表格sheet_weather_3,查找键 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)

接下来查询天气小于30华氏度的城市,代码如下:

import pymongo

client = pymongo.MongoClient('localhost', 27017)

book_weather = client['weather']

sheet_weather = book_weather['sheet_weather_2']

for item in sheet_weather.find():

tmp = item['HeWeather6'][0]['now']['tmp']

sheet_weather.update_one({'_id':item['_id']},{'$set':{'HeWeather6.0.now.tmp':int(tmp)}})

for item in sheet_weather.find({'HeWeather6.0.now.tmp':{'$lt':30}}):

print(item['HeWeather6'][0]['basic']['location'])

查询三天里,天气最低温小于30摄氏度的城市名,代码如下:

import pymongo

client = pymongo.MongoClient('localhost', 27017)

book_weather = client['weather']

sheet_weather = book_weather['sheet_weather_1']

for item in sheet_weather.find():

for i in range(3): # 因为数据需要3天的天气预报,因此要循环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':{'$lt':30}}):

print(item['HeWeather6'][0]['basic']['location'])

使用updata方法,将表中最低气温数据修改为数据值型,更新的数据用sheet_weather,其中update_one方法用于指定更新一条数据,这里的第一个参数是{'_id':item['_id']},表示要更新的查询条件,第二段参数:{'$set':{'HeWeather6.0.daily_forecast.{}.tmp_min'.format(i):int(tmp)},表示要更新的信息,set 是MongoDB中的一个修改器,用于指定一个键并更新键值,若键不存在则创立一个键。

常用的修改器还有:

$inc,$unset,$push

修改器$inc,可以对文档某个值为数字型(只能为满足要求的数字)的键进行增减操作。

修改器$unset,用于删除键。

修改器$push,向文档的某个数组类型的键添加一个数组元素,不过滤重复的数据。添加的时候,若键存在,要求键值类型必须是数组;若键不存在,则创建数组类型的键。

数据更新完毕后再用find方法查找照数据,代码如下:

for item in sheet_weather.find({'HeWeather6.0.daily_forecast.tmp_min':{'$lt':30}}):

print(item['HeWeather6'][0]['basic']['location'])

其中,

math?formula=lt%E3%80%81lte、

math?formula=gt%E5%92%8Cgte,分别表示符号<、<=、>和>=。

Logo

这里是“一人公司”的成长家园。我们提供从产品曝光、技术变现到法律财税的全栈内容,并连接云服务、办公空间等稀缺资源,助你专注创造,无忧运营。

更多推荐