2.1 python使用MongoDB 示例代码
import pymongo
client = pymongo.MongoClient('localhost', 27017) # MongoDB 客户端
walden = client['walden'] # 数据库中创建的名称
sheet_tab = walden['sheet_tab'] # 创建Table
# 演示代码1
# path = '/Users/qiongyanzhu/Documents/Plan-for-combating-master/week2/2_1/2_1code_of_video/walden.txt'
# with open(path, 'r') as f:
# lines = f.readlines()
# for index, line in enumerate(lines):
# data = {
# 'index': index,
# 'line': line,
# 'words': len(line.split())
# }
# print(data)
# sheet_tab.insert_one(data)
# 演示代码2
# for item in sheet_tab.find({'words': 0}):
# print(item)
# 演示代码3
# $lt/$lte/$gt/$gte/$ne
for item in sheet_tab.find({'words': {'$lt': 5}}):
print(item)
# 演示代码4
for item in sheet_tab.find():
print(item['line'])
from bs4 import BeautifulSoup
import requests
import pymongo client = pymongo.MongoClient('localhost', 27017)
xiaozhu = client['xiaozhu']
sheet_tab = xiaozhu['sheet_tab'] url_as = ['http://bj.xiaozhu.com/search-duanzufang-p{}-0/'.format(str(number)) for number in range(1, 4)] def insert_house_info(url_s):
for url_a in url_s:
# 获取页面数据
wb_data = requests.get(url_a)
# 采用lxml解析引擎,解析数据
soup = BeautifulSoup(wb_data.text, 'lxml')
prices = soup.select('span.result_price')
titles = soup.select('#page_list > ul > li > div.result_btm_con.lodgeunitname > div > a > span')
urls = soup.select('#page_list > ul > li > div.result_btm_con.lodgeunitname') for price, title, url in zip(prices, titles, urls):
info = {
'price': int(price.get_text()[1:len(price.get_text())-2]),
'title': title.get_text(),
'url': url.get('detailurl')
}
# print(info)
sheet_tab.insert_one(info) def find_house():
for info in sheet_tab.find({'price': {'$gt': 500}}):
print(info) insert_house_info(url_as)
find_house()
2.1 python使用MongoDB 示例代码的更多相关文章
- Python 驱动 MongoDB 示例(PyMongo)
Python 的MongoDB驱动 pymongo ,使用pip Install pymongo安装即可 最近发现网上的很多实例已经过时了,在此自我探究记录下来. 编写一个接口类来支持MongoDB的 ...
- Python操作MongoDB代码示例
import pymongo #pip install pymongo安装python操作mongodb的模块 myclient=pymongo.MongoClient(host='127.0.0.1 ...
- mailgun 发邮件示例代码Python版
1 首先到mailgun官网注册账号,并激活账号 点击domains,进入默认的域名,最底下那个sandbox域名就是默认的测试域名 如果自己有域名,也可以添加自己的域名测试,具体参考:ssr pan ...
- Python 连接MongoDB并比较两个字符串相似度的简单示例
本文介绍一个示例:使用 pymongo 连接 MongoDB,查询MongoDB中的 字符串 记录,并比较字符串之间的相似度. 一,Python连接MongoDB 大致步骤:创建MongoClient ...
- python开源项目及示例代码
本页面是俺收集的各种 Python 资源,不定期更新. 下面列出的各种 Python 库/模块/工具,如果名称带超链接,说明是第三方的:否则是 Python 语言内置的. 1 算法 1.1 字符串处理 ...
- python开源项目及示例代码(转)
本页面是俺收集的各种 Python 资源,不定期更新. 下面列出的各种 Python 库/模块/工具,如果名称带超链接,说明是第三方的:否则是 Python 语言内置的. 1 算法 1.1 字符串处理 ...
- 传递命令行参数示例代码 (C 和 Python)
C语言 在 C 语言中, 使用 main 函数的输入参数 argc 和 argv 传入命令行参数. argc 为 int 类型, 表示传入命令行参数的个数 (argument count); argv ...
- python中实现延时回调普通函数示例代码
python中实现延时回调普通函数示例代码 这篇文章主要给大家介绍了关于python中实现延时回调普通函数的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的 ...
- python 示例代码1
第一章 python基础一 在此不再赘述为什么学习python这门编程,网上搜索一箩筐.我在此仅说一句python的好,用了你就会爱上它. 本python示例代码1000+带你由浅入深的了解pyth ...
随机推荐
- jQuery基础_4
dom对象就是jquery对象的数组组成部分jquery对象和dom对象的转化jquery对象-->dom对象$()[下标]dom对象-->jquery对象$(dom对象) jquery框 ...
- SharePoint 2013 工作流之年假审批Designer配置篇
本文介绍SharePoint 2013 使用Designer工具,设计年假审批工作流,由于流程所用的条件和操作都比较简单,所以演示为主,最后附流程图和流程的文本图,有兴趣的可以参照实验.如果对于Des ...
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q136-Q138)
Question 136You need to create a custom content type and specify the content type ID.What should you ...
- iOS之百度导航SDK的坐标转换
百度导航 iOS SDK的坐标转换代码示例,有需要的朋友可以参考下. //导航坐标--------------> 地图坐标 //假设从导航sdk取到了一个点坐标是(116.304847, 40. ...
- Golang(笔记) 面向对象
package main import ( "fmt" ) //对象定义 type Rect struct{ x,y float64 width ,height float64 } ...
- 来自沪江、滴滴、蘑菇街架构师的 Docker 实践分享
架构师小组交流会是由国内知名公司架构师参与的技术交流会,每期选择一个时下最热门的技术话题进行实践经验分享. Docker 作为当前最具颠覆性的开源技术之一,其轻量虚拟化.可移植性是 CI/CD.Dev ...
- Android中Listview点击item不变颜色以及设置listselector 无效
Android中Listview点击item不变颜色以及设置listselector 无效 这是同一个问题,Listview中点击item是会变颜色的,因为listview设置了默认的listsele ...
- Wireshark设置interface 时提示“There are no interfaces on which a capture can be done ”
Wireshark设置interface 时提示“There are no interfaces on which a capture can be done ” 解决方法: 今天在电脑上安装了WIR ...
- 常用API——Math对象型、Number型
Math.abs(num) : 返回num的绝对值 Math.acos(num) : 返回num的反余弦值 Math.asin(num) : 返回num的反正弦值 Math.atan(num) : 返 ...
- leveldb源码分析--SSTable之Compaction
对于compaction是leveldb中体量最大的一部分,也应该是最为复杂的部分,为了便于理解我们首先从一些基本的概念开始.下面是一些从doc/impl.html中翻译和整理的内容: Level 0 ...