(1用API爬取天气预报数据)Python爬虫与数据清洗的进化

1、一个简单网页源代码爬取

import requests
url='http://www.cntour.cn/'
strhtml=requests.get(url)
print(strhtml.text[:50]) #提取前50个字符

2、 使用Beautiful Soup解析网页,可以顺便安装一下lxml库,功能强大,速度更快。

  • 复制CSS选择器路径。

  • 将css选择器路径复制到soup.select中。
import requests
from bs4 import BeautifulSoup
url='http://www.cntour.cn/'
strhtml=requests.get(url)
soup=BeautifulSoup(strhtml.text,'lxml')
data=soup.select('#main > div > div.mtop.firstMod.clearfix > div.leftBox > div:nth-child(2) > ul > li > a')
for item in data:
    result={
        'Title':item.get_text(),
        'Link':item.get('href'), 
    }
    print(result)

3、使用正则表达式提取文章数字ID

\d 匹配数字  +匹配前一个字符一次或多次

使用re库的findall方法,第一个参数为正则表达式,第二个参数表示要提取的文本。

import requests
from bs4 import BeautifulSoup
import re
url='http://www.cntour.cn/'
strhtml=requests.get(url)
soup=BeautifulSoup(strhtml.text,'lxml')
data=soup.select('#main > div > div.mtop.firstMod.clearfix > div.leftBox > div:nth-child(2) > ul > li > a')
for item in data:
    result={
        'ID': re.findall('\d+', item.get('href')),
        'Title':item.get_text(),
        'Link':item.get('href'), 
    }
    print(result)

4、用API爬取天气预报数据 

网址:https://www.heweather.com/

删除顶部不需要的数据使用remove方法,通过一个列表元素item循环输出每一行前14个字符(城市编码)

import requests
url='https://cdn.heweather.com/china-scenic-list.txt'
strhtml=requests.get(url)
strhtml.encoding='utf-8'
data=strhtml.text
new_data=data.split('\n')
for i in range(3):
    new_data.remove(new_data[0])
for item in new_data:
    print(item[0:14])

调用接口获取数据

import requests
import time
with open('outcity.txt','r')as f:
    list=f.readlines()
for i in range(2):
    list.remove(list[0])
for item in list:
    print(item[0:12])
    url='https://free-api.heweather.net/s6/weather?location='+item[0:12]+'&key=5ce6464c17824a6ea9aa12dd749e2bc5'
    strhtml=requests.get(url)
    strhtml.encoding='utf-8'
    time.sleep(1)
    print(strhtml.text)

将返回的json数据解析出来,通过观察路径接下来三天的最高温度在 ["HeWeather6"][0]["daily_forecast"][n]["tmp_max"]下面,其中n表示分节点

    dic=strhtml.json()
    for item in dic["HeWeather6"][0]["daily_forecast"]:
        print(item["tmp_max"])

将数据上传到mongodb中

import requests
import time
import pymongo #加载pymongo库
client=pymongo.MongoClient('localhost',27017) #建立连接
book_weather=client['weather'] #新建weather数据库
sheet_weather=book_weather['sheet_weather_3'] #在weather库中新建sheet_weather_3表
with open('outcity.txt','r')as f:
    list=f.readlines()
for i in range(2):
    list.remove(list[0])
for item in list:
    print(item[0:12])
    url='https://free-api.heweather.net/s6/weather?location='+item[0:12]+'&key=5ce6464c17824a6ea9aa12dd749e2bc5'
    strhtml=requests.get(url)
    strhtml.encoding='utf-8'
    time.sleep(1)
    dic=strhtml.json()
    sheet_weather.insert_one(dic)

在pycharm中下载mongo plus插件可以便捷查看数据。 

MongoDB数据库查询(北京的天气数据)

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)

查询最高温度低于20摄氏度的城市 $lt:< $lte:<= $gt:> $gte:>=,其中update_one方法用于指定更新一条数据,第一个参数{'_id:item['_id']}表示要更新的查询条件。第二个参数表示要更新的信息,$set是MongoDB中的一个修改器,用于制定一个键并更新键值,若键不存在则创建一个件键。 

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_max=item["HeWeather6"][0]["daily_forecast"][0]['tmp_max']
    sheet_weather.update_one({'_id':item['_id']},{'$set':{'HeWeather6.0.daily_forecast.{}.tmp_max'.format(0):int(tmp_max)}})
    for item in sheet_weather.find({'HeWeather6.0.daily_forecast.0.tmp_max':{'$lt':20}}):
        print(item['HeWeather6'][0]['basic']['location'])

 

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值