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}})

  

相关文章:

  • 2021-12-26
  • 2022-01-13
  • 2021-12-21
  • 2021-12-10
  • 2021-10-25
  • 2021-07-02
  • 2022-12-23
  • 2021-12-24
猜你喜欢
  • 2021-12-03
  • 2021-12-11
  • 2022-12-23
  • 2021-12-12
  • 2022-12-23
  • 2022-01-23
  • 2021-11-18
相关资源
相似解决方案