使用mongoDB

下载地址:https://www.mongodb.com/dr/fastdl.mongodb.org/win32/mongodb-win32-x86_64-2008plus-ssl-4.0.9.zip/download

百度链接:https://pan.baidu.com/s/1xhFsENTVvU-tnjK9ODJ7Ag 密码:ctyy

mongoDB的安装

https://www.cnblogs.com/iamluoli/p/9254899.html

可视化Robo3T

下载:https://robomongo.org/

安装pymongo

利用anacode安装whl:https://www.lfd.uci.edu/~gohlke/pythonlibs/#pymonge

创建数据库和表单

import pymongo
# 创建本地环境的mongo
client = pymongo.MongoClient('localhost', 27017)
# 给数据库起名 前面的walden为python环境里的名称,后面的walden为数据库的名称
# 两者建议为同一值方便操作
walden = client["walden"]
# 在文件下创建表单
sheet1 = walden["sheet1"]

读取文件信息

# 读取文件
path = "C:/Users/Y/Desktop/sheet1.txt"
with open(path, "r") as f:
   lines = f.readlines()
   # enumerate 可同时获取列表中的数据和数据下标
   for index,line in enumerate(lines):
       data = {
           "index": index,
           "message": line.strip(),
           "num": len(line.strip())
      }
       print(data)
{'index': 0, 'message': 'sad', 'num': 3}
{'index': 1, 'message': 'sdadasda', 'num': 8}
{'index': 2, 'message': 'asdsad', 'num': 6}

写入数据库

遇到的问题:pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [WinError 10061] 由于目标计算机积极拒绝,无法连接。

由于没有启动本地的MongoDB服务引起。

以管理员的身份打开cmd 输入net start MongoDB

显示MongoDB 服务正在启动 .. MongoDB 服务已经启动成功。即可。

然后插入数据

sheet1.insert_one(data)

展示数据库中的数据

for item in sheet1.find():
   print(item)
{'_id': ObjectId('5cbe7d5a0cc48d12680ac2fa'), 'index': 413, 'words': 187, 'message': "Within a few miles of Keswick, we passed along at the foot of Saddleback, and by the entrance of the Vale of St. John, and down the valley, on one of the slopes, we saw the Enchanted Castle. Thence we drove along by the course of the Greta, and soon arrived at Keswick, which lies at the base of Skiddaw, and among a brotherhood of picturesque eminences, and is itself a compact little town, with a market-house, built of the old stones of the Earl of Derwentwater's ruined castle, standing in the centre,—the principal street forking into two as it passes it. We alighted at the King's Arms, and went in search of Southey's residence, which we found easily enough, as it lies just on the outskirts of the town. We inquired of a group of people, two of whom, I thought, did not seem to know much about the matter; but the third, an elderly man, pointed it out at once,—a house surrounded by trees, so as to be seen only partially, and standing on a little eminence, a hundred yards or so from the road."}
......

发现其中被自动添加了一个键名为id的键值对,是mongoDb自带的数据索引,防止数据重复。

或者利用可视化数据库Robo展示或Mongo Explorer展示,就不一一赘述。

筛选功能

# $lt/$lte/$gt/$gte/$ne 依次等价于< , <= , > , >= , !=
for item in sheet1.find({"words": {"$lt":5}}):
   print(item)

The fifth day of Crawler learning的更多相关文章

  1. The sixth day of Crawler learning

    爬取我爱竞赛网的大量数据 首先获取每一种比赛信息的分类链接 def get_type_url(url):    web_data = requests.get(web_url)    soup = B ...

  2. The fourth day of Crawler learning

    爬取58同城 from bs4 import BeautifulSoupimport requestsurl = "https://qd.58.com/diannao/35200617992 ...

  3. The third day of Crawler learning

    连续爬取多页数据 分析每一页url的关联找出联系 例如虎扑 第一页:https://voice.hupu.com/nba/1 第二页:https://voice.hupu.com/nba/2 第三页: ...

  4. The second day of Crawler learning

    用BeatuifulSoup和Requests爬取猫途鹰网 服务器与本地的交换机制 我们每次浏览网页都是再向网页所在的服务器发送一个Request,然后服务器接受到Request后返回Response ...

  5. The first day of Crawler learning

    使用BeautifulSoup解析网页 Soup = BeautifulSoup(urlopen(html),'lxml') Soup为汤,html为食材,lxml为菜谱 from bs4 impor ...

  6. Machine and Deep Learning with Python

    Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstiti ...

  7. Node.js Learning Paths

    Node.js Learning Paths Node.js in Action Node.js Expert situations / scenario Restful API OAuth 2.0 ...

  8. 【Machine Learning】KNN算法虹膜图片识别

    K-近邻算法虹膜图片识别实战 作者:白宁超 2017年1月3日18:26:33 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本系列文章是作者结 ...

  9. 【Machine Learning】Python开发工具:Anaconda+Sublime

    Python开发工具:Anaconda+Sublime 作者:白宁超 2016年12月23日21:24:51 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现 ...

随机推荐

  1. [考试维护]之IIS发布 标签: iis 2015-06-07 22:11 627人阅读 评论(18) 收藏

    考试维护也进行了一段时间了,总结一下这段时间学习到的东西,今天写一下在服务器上如何发布IIS,一开始,我们准备了两台服务器,一台Win Server2003的服务器(IIS版本6.0),另一台是Win ...

  2. 中国联通与阿里云达成合作,推动5G+新媒体产业发展

    4月24日在中国联通合作伙伴大会上,阿里云与中国联通签署合作协议,未来双方将基于各自优势,聚焦5G时代下的超高清视频发展. 随着5G时代到来,视频不再被网速制约,超短延时.计算节点下沉等特性将更高清. ...

  3. Android 在图片的指定位置添加标记

    这些天,项目里加了一个功能效果,场景是: 假如有一个家居图片,图片里,有各样的家居用品: 桌子,毛巾,花瓶等等,需要在指定的商品处添加标记,方便用户直接看到商品,点击该标记,可以进入到商品详情页 .实 ...

  4. 一个项目看java TCP/IP Socket编程

    前一段时间刚做了个java程序和网络上多台机器的c程序通讯的项目,遵循的是TCP/IP协议,用到了java的Socket编程.网络通讯是java的强项,用TCP/IP协议可以方便的和网络上的其他程序互 ...

  5. C. Tokitsukaze and Duel 前缀维护

    枚举每一个连续的K的第一个位置,如果是先手胜利,那么前[1 , i-1 ]和[ i+k , n ]区间要么全是0,要么全是1 如果能够平局,那么肯定是[1,i-1],以及[ i+k , n]中有两种情 ...

  6. 学习CSS预处理器Less

    1.Sass与Less的区别:Sass与Less的区别 2.Less的使用:Less的使用 3.Less的相关网址:Less.js.Less中文网 Less的HSL函数-lighten():HSL函数 ...

  7. Install Openjdk11 to Ubuntu 18.04 LTS

      Ubuntu 18.04 LTS系统上通过sudo apt install openjdk-11-*命令安装的jdk11版本依然是jdk10,怎么样才能安装openjdk 11呢,今天,我们就来完 ...

  8. 使用属性position:fixed的时候如何才能让div居中

    css: .aa{ position: fixed; top: 200px; left: 0px; right: 0px; width: 200px; height: 200px; margin-le ...

  9. redux【react】

    首先介绍一下redux就是Flux的一种进阶实现.它是一个应用数据流框架,主要作用应用状态的管理 一.设计思想: (1).web应用就是一个状态机,视图和状态一一对应 (2).所有的状态保存在一个对象 ...

  10. HDU 1864 01背包、

    这题题意有点坑阿.感觉特别模糊. 我开始有一点没理解清楚.就是报销的话是整张整张支票报销的.也是我傻逼了 没一点常识 还有一点就是说单张支票总额不超过1000,每张支票中单类总额不超过600,我开始以 ...