The fifth day of Crawler learning
使用mongoDB
百度链接:https://pan.baidu.com/s/1xhFsENTVvU-tnjK9ODJ7Ag 密码:ctyy
mongoDB的安装
https://www.cnblogs.com/iamluoli/p/9254899.html
可视化Robo3T
安装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的更多相关文章
- The sixth day of Crawler learning
爬取我爱竞赛网的大量数据 首先获取每一种比赛信息的分类链接 def get_type_url(url): web_data = requests.get(web_url) soup = B ...
- The fourth day of Crawler learning
爬取58同城 from bs4 import BeautifulSoupimport requestsurl = "https://qd.58.com/diannao/35200617992 ...
- The third day of Crawler learning
连续爬取多页数据 分析每一页url的关联找出联系 例如虎扑 第一页:https://voice.hupu.com/nba/1 第二页:https://voice.hupu.com/nba/2 第三页: ...
- The second day of Crawler learning
用BeatuifulSoup和Requests爬取猫途鹰网 服务器与本地的交换机制 我们每次浏览网页都是再向网页所在的服务器发送一个Request,然后服务器接受到Request后返回Response ...
- The first day of Crawler learning
使用BeautifulSoup解析网页 Soup = BeautifulSoup(urlopen(html),'lxml') Soup为汤,html为食材,lxml为菜谱 from bs4 impor ...
- Machine and Deep Learning with Python
Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstiti ...
- Node.js Learning Paths
Node.js Learning Paths Node.js in Action Node.js Expert situations / scenario Restful API OAuth 2.0 ...
- 【Machine Learning】KNN算法虹膜图片识别
K-近邻算法虹膜图片识别实战 作者:白宁超 2017年1月3日18:26:33 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本系列文章是作者结 ...
- 【Machine Learning】Python开发工具:Anaconda+Sublime
Python开发工具:Anaconda+Sublime 作者:白宁超 2016年12月23日21:24:51 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现 ...
随机推荐
- mySQL start service失败终极解决办法
start service失败 原因是电脑没删干净.具体1.先卸载2.计算机“搜索”所有MySQL文件 注意隐藏文件也可以搜索出来全部删除.3.清除注册表MySQL及子项.4.防火墙的问题 不要勾 ...
- oralce GROUPING
/*从上面的结果中我们很容易发现,每个统计数据所对应的行都会出现null, 如何来区分到底是根据那个字段做的汇总呢,grouping函数判断是否合计列!*/ select decode(groupin ...
- HDU_1087-Super Jumping! Jumping! Jumping!
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- lattice planner 规划详解
大家好,我是来自百度智能驾驶事业群的许珂诚.今天很高兴能给大家分享Apollo 3.0新发布的Lattice规划算法. Lattice算法隶属于规划模块.规划模块以预测模块.routing模块.高精地 ...
- Celery后台任务
Celery 在程序运行过程中,经常会遇到一些耗时耗资源的任务,为了避免这些任务阻塞主进程的运行,我们会采用多线程或异步任务去处理.比如在Web中需要对新注册的用户发一封激活邮件来验证账户,而发邮件本 ...
- Where是深复制
从一个List中通过Where过滤出来的子List或Fist出来的单独元素,与原List是深复制关系.也就是说修改子list,或单独元素,并不会影响原List中的对象. 如:Geom g = Geom ...
- 「BZOJ1005」[HNOI2008] 明明的烦恼
「BZOJ1005」[HNOI2008] 明明的烦恼 先放几个prufer序列的结论: Prufer序列是一种对有标号无根树的编码,长度为节点数-2. 具体存在无根树转化为prufer序列和prufe ...
- Android的headerView和emptyView共存问题
今天做项目的时候,准备优化下ListView相关的东西,于是乎,需要做一个当列表无数据时,空的提醒页面.这个自然想到的是ListView的setEmptyView()方法,于是顺手就写了,可是,当我为 ...
- ODT 珂朵莉树 入门
#include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> ...
- 第三期 预测——Frenet 坐标
Frenet坐标 在讨论过程模型之前,我们应该提到“Frenet Coordinates”,它是一种以比传统x,y笛卡尔坐标更直观的方式表示道路位置的方式. 用Frenet坐标,我们使用变量 s和d描 ...