• Python中的数据库连接与查询——使用pymongo


    pymongo是在Python环境下使用MongoDB的方法。

    以某电商网站搜索“连衣裙”的第一页商品数据抓取下来并存入MongoDB数据库。

    import requests
    import pymongo
    client = pymongo.MongoClient('localhost',27017)
    # 创建数据库
    taobao = client['taobao']
    # 新建表
    search_result = taobao['search_result']
    # 爬取数据
    url = '***'
    strhtml = requests.get(url)
    result = strhtml.json()
    for item in result['listItem']:
        json_data = {
            'title':item['title'],
            'price':float(item['price']),
            'sold':int(item['sold']),
            'location':item['location']
    }
    # 表中写入数据
    search_result.insert(json_data)
    

      

    查询位置在“浙江 杭州”,并且价格大于100元的商品数据

    for item in search_result.find({"location":"浙江 杭州","price":{'$gt':100}})
        print(item)
    

      

    将位置“浙江 杭州”改为“浙江”

    search_result.update({"location":"浙江 杭州"},{"$set":{"location":"浙江”}})
    

      

    删除销量小于1000件的商品数据

    search_result.remove({"sold":{'$lt':1000}})
    

      

  • 相关阅读:
    Flume基础(一):概述
    Hive高级(2):优化(2) 表的优化
    ospf命令
    Verizon 和某 BGP 优化器如何在今日大范围重创互联网
    BGP数据中心鉴别方法
    多线BGP鉴定
    mpls ldp neighbor 和loopbak
    ospf默认路由
    ospf
    ubuntu cloud init获取元数据失败
  • 原文地址:https://www.cnblogs.com/zhuozige/p/13131979.html
Copyright © 2020-2023  润新知