• 吴裕雄--天生自然PYTHON爬虫:安装配置MongoDBy和爬取天气数据并清洗保存到MongoDB中


    1、下载MongoDB

    官网下载:https://www.mongodb.com/download-center#community

     

     上面这张图选择第二个按钮

     上面这张图直接Next

     

     把bin路径添加到path中,如下图:

    在安装路径下自己创建一个文件mongo.conf,配置内容如下:

    #数据库路径
    dbpath=E:mongodbdata
    #日志输出文件路径
    logpath=E:mongodblogmongo.log
    #错误日志采用追加模式
    logappend=true
    #启用日志文件,默认启用
    journal=true
    #这个选项可以过滤掉一些无用的日志信息,若需要调试请使用设置为false
    quiet=true
    #端口号默认为27017
    port=27017

    启动MongoDB服务

    打开cmd命令行护着用Windows+R键打开,输入cmd

    进入mongo安装路径的bin目录下

    输入如下命令启动MongoDB:mongod --dbpath "E:mongodbdata"

     当你点击回车的时候,出现上面界面,说明已经成功了。

    配置本地windows mongodb 服务

    这样可设置为 开机自启动,可直接手动启动关闭,可通过命令行net start MongoDB 启动。该配置会大大方便。也不要在进入bin的目录下启动了

    在mongodb新建配置文件mongo.config,这个是和bin目录同级的,该配置文件内容在上面可以找到。

    用管理员身份打开cmd,左上角会出现管理员三个字,然后一次进入你的bin的目录下G:mongodbin,这个一定要有管理员的身份去打开,否则执行下面命令会一直报错

    输入:mongod --dbpath "E:mongodbdata" --logpath "E:mongodblogmongo.log" --install --serviceName "MongoDB"

    如果输入次命令出现错误的话,先删除服务sc delete MongoDB,再次输入上个命令就好了

    这样的话,mongodb服务Windows已经配置好了,我们可以不用进入bin的目录下启动MongoDB了

     

     在浏览器输入http://localhost:27017,如果在浏览器中出现下面一段英文说明成功了

    接下来在pycharm安装mongo plugo

     

    安装好后重启IDE

    重启后在右边能看到

    创建一个mongo server

     测试连接成功,点击OK保存设置就可以了。

     下面这段代码将爬取到的天气数据放到MongoDB中。

    import time
    import pymongo
    import requests
    
    client = pymongo.MongoClient('localhost',27017)
    #在MongoDB中新建名为weather的数据库
    book_weather = client['weather']
    #在weather库中创建名为sheet_weather_3的表
    sheet_weather = book_weather['sheet_weather_3']
    
    #爬取天气数据
    url = 'https://cdn.heweather.com/china-city-list.txt'
    response = requests.get(url)
    response.encoding='utf8'
    data = response.text
    data_1 = data.split('
    ')
    #去除前三行不要的数据
    for i in range(3):
        data_1.remove(data_1[0])
        
    temp = 1
    for item in data_1:
        url = 'https://free-api.heweather.net/s6/weather/forecast?location='+item[1:13]+'&key=232ab5d4b88e46bcb8bd8c06d49ebf91'
        strhtml = requests.get(url)
        time.sleep(3)
        dic = strhtml.json()
        if(temp>3):
            #向sheet_weather_3表写入当前这条数据
            sheet_weather.insert_one(dic)else:
            temp+=1

    运行代码

     可以看到:爬取到的数据都以json的格式保存到mongodb数据库了。

    接下来查询Mongodb数据库。代码如下:

    import pymongo
    
    client = pymongo.MongoClient('localhost',27017)
    book_weather = client['weather']
    sheet_weather = book_weather['sheet_weather_3']
    #查找HeWeather6.basic.admin_area值为北京的数据。
    for item in sheet_weather.find({'HeWeather6.basic.admin_area':'北京'}):
        print(item)

     接下来查询最大气温大于0度的城市名称。代码如下:

    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_max = item['HeWeather6'][0]['daily_forecast'][i]['tmp_max']
            #使用update方法,将表中最低气温数据修改数据值
            sheet_weather.update_one({'_id':item['_id']},{'$set':{'HeWeather6.0.daily_forecast.{}.tmp_max'.format(i):int(tmp_max)}})
            
    #找出最高气温不小于0度的城市
    for item in sheet_weather.find({'HeWeather6.daily_forecast.tmp_max':{'$gte':0}}):
        print(item['HeWeather6'][0]['basic']['admin_area'])

     可以看出北京这几天的天气最高气温都是大于零度的。

    mongodb中比较符:

    小于:$lt

    小于等于:$lte

    大于:$gt

    大于等于:$gte

  • 相关阅读:
    linux centos 8 为.net core 添加进程守护 Supervisor
    vue-quill-editor安装及使用:自定义工具栏和自定义中文字体,把字体写在html的style中
    nginx参数
    k8s pod 挂载glusterfs报错
    Vue跨域问题解决
    CSS导航菜单(二级菜单)
    CSS导航菜单(一级菜单)
    微信小程序开发正常,真机预览模式错误
    uniapp获取用户OpenId及用户详情
    uniapp 获取用户手机号
  • 原文地址:https://www.cnblogs.com/tszr/p/12192354.html
Copyright © 2020-2023  润新知