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 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现 ...
随机推荐
- Unity3dShader学习合集暂存
http://www.cnblogs.com/flappy/archive/2012/08/10/2631348.html 1. Unity3d的參考手冊, 里面涵盖Unity3d官方的描写叙述 ht ...
- sql查询报java.sql.SQLException: Column 'LC_ID' not found 的错误实际上是mysql在hibernate别名的问题
报java.sql.SQLException: Column 'LC_ID' not found 的错误实际上是mysql在hibernate别名的问题 我的查询sql是 String sql2 =& ...
- UVa-10986_Sending email (向前星+Dijkstra)
题意:给你点.边,求起点到终点的最短距离. 题解:由于题目的数据量特别大,所以需要用邻接表来存边,之后对Dijkstra算法稍微魔改一下就可以了,本来以为会超时,做好了打堆优化的准备,结果卡时间过了, ...
- oracle函数 TRANSLATE(c1,c2,c3)
[功能]将字符表达式值中,指定字符替换为新字符 [说明]多字节符(汉字.全角符等),按1个字符计算 [参数] c1 希望被替换的字符或变量 c2 查询原始的字符集 c3 替换新的字符集,将 ...
- VSCode配置启动Vue项目
下载安装并配置VSCode 随便百度上搜个最新的VSCode安装好后,点击Ctrl + Shit + X打开插件扩展窗口进行插件扩展,这里要安装两个插件. 1.vetur插件的安装 该插件是vue文件 ...
- 学习layui框架
Layui是一款功能齐全的前端框架,需要引入对应的CSS文件和JS文件,附属官网链接:Layui官网
- windows 关闭端口被占用脚本
cmd 关闭进程java taskkill /F /IM java.exe taskkill /f /im java.exe 如何用dat批处理文件关闭某端口对应程序-Windows自动化命令 如何用 ...
- Spring < context:annotation-config> 、< context:component-scan>、< mvc:annotation-driven />注解配置
Spring 中在使用注解(Annotation)会涉及到< context:annotation-config> 和 < context:component-scan>配置, ...
- 【转载】.NET中使用Redis
Redis是一个用的比较广泛的Key/Value的内存数据库,新浪微博.Github.StackOverflow 等大型应用中都用其作为缓存,Redis的官网为http://redis.io/. 最近 ...
- java声明异常(throws)
在可能出现异常的方法上声明抛出可能出现异常的类型: 声明的时候尽可能声明具体的异常,方便更好的处理. 当前方法不知道如何处理这种异常,可将该异常交给上一级调用者来处理(非RuntimeExceptio ...