python爬虫入门实战---------一周天气预报爬取_《python3爬虫、数据清洗与可视化实战》第三章 用API爬取天气预报数据...

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,分别表示符号<、<=、>和>=。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
安居客是一个房地产信息网站,为了获取其中的数据,可以使用Python编写爬虫程序进行网页内容的抓取。通过分析网页的结构和规律,可以从安居客网站上获取所需的数据爬虫程序可以通过请求网页的URL,获取网页的HTML源码,然后使用Python的相关库(如BeautifulSoup)对HTML进行解析,提取出需要的数据。 在获取到数据之后,需要进行数据清洗数据清洗是指对采集到的原始数据进行清理、处理,使其符合数据分析的要求。例如,去除重复数据、处理缺失值、剔除异常值等。通过使用Python数据处理库(如pandas)可以方便地进行数据清洗的操作。 数据清洗完成后,可以进行数据分析。数据分析是指对清洗后的数据进行统计、计算、挖掘等操作,以获取其中的有价值的信息和规律。Python中有很多用于数据分析的库,如numpy、scipy、matplotlib等。可以使用这些库进行数据的统计分析、机器学习算法的实施、模型的构建等。 最后,为了更好地展示分析结果,可以进行数据可视化数据可视化是指使用图表、图像等形式将数据进行直观、易懂的展示。Python中的matplotlib、seaborn等库提供了丰富的图表绘制功能,可以将数据转换为直观的图表,以便更好地帮助用户理解和分析数据。 综上所述,通过安居客数据爬取数据清洗数据分析可视化,可以获取并理解网站上房地产相关的有价值信息,为用户提供更好的数据分析与决策支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值