python 爬虫开发从入门到实践 读书笔记(二)

栏目: Python · 发布时间: 6年前

内容简介:方法一方法二方法三

PyMongo

pip instal pymongo

pymongo 使用

from pymongo import MongoClient
client = MongoClient() # 默认本机什么都不用添加
client = MongoClient('mongodb://@IP:PORT')
client = MongoClient('mongodb://用户名:密码@IP:PORT')

初始化数据与集合

方法一

from pymongo import MongoClient
client = MongoClient()
database = client.db1 # 库
collection = db1.spider # 集合

方法二

from pymongo import MongoClient
client = MongoClient()
database = client['db1']
collection = database['spider']

方法三

databast_name_list = ['db1','db2','db3']
for each_db in database_name_list:
    database = client[each_db]
    collection = database.test

插入数据

from pymongo import MongoClient
client = MongoClient()
database = client['db1']
collection = database['spider']
data = {'id':'123', 'name':'username01','age':20}
collection.insert(data)
more_data = [{数据1},{数据2}] # 插入多个数据
collection.insert(more_data)

普通查找

find(查询语句,返回字段)
find_one(查询语句,返回字段)
content = collection.find() # 不加参数, 显示所有
content = collection.find({'age':20})
content = collection.find({'age':20}, {'_id':0, 'username':1,'age':1}) # 第二个参数指定返回的内容. 这个参数是个字典, key 是字段名称, value是0或1, 0表示不返回这个字段, 1表示返回的字段. 其中_id 比较特殊, 必须人工知道你个值为0, 这样才不会返回. 对于其他数据,应该统一使用返回或者不返回

逻辑查询

  • $gt 大与
  • $lt 小于
  • $gte 大于等于
  • $lte 小于等于
  • $eq 等于
  • $ne 不等于
collection.find({'age':{'$gt':20}}) # 查询 age>20的记录
collection.find({'age':{'$gte':20,'$lte':40}}) # 查询20≤age≤40

查询结果排序

collection.find().sort('列名',1或-1) # 1表示升序,-1表示降序
collection.find({'age':{'$gte':20,'$lte':40}}).sort('age',1)

更新记录

collection.update_one(参数1, 参数2)
collection.update_many(参数1, 参数2)

# 将第一个年龄为20的人, 改名字
collection.update_one({'age':20}, {'$set':{'name':'username01'}})
# 将所有年龄为20的人, 改年龄
collection.update_many({'age':20}, {'$set':{'age':40}})

删除记录

collection.delete_one(参数)
collection.delete_many(参数)

collection.delete_one({'age':20}) # 删除一条
collection.delete_many({'age':20}) # 删除所有符合条件的

查询结果去重

collection.distinct(列名)
# 查询有多少个不同年龄的人
collection.distinct('aget')

交互环境的使用

keys *
  • lpush 从左侧写数据到列表中
  • lpop 从左侧读取数据
  • rpush
  • rpop
  • llen 查看列表的长度
  • lrange key start end # lrange test 0 3 读取下标 0-1-2-3 四个
lpush  key value1 value2 ...

eg:

127.0.0.1:6379> lpush c6 "url" 
(integer) 1
127.0.0.1:6379> lpush test "hello" "world"
(integer) 2
127.0.0.1:6379> lpop test
"world"

python 一样, 没有顺序, 值不重复

sadd key value1 value2 value3 ...
127.0.0.1:6379> sadd test_set "https://baidu.com"
(integer) 1
127.0.0.1:6379> sadd test_set 1 2 3 4 5 6 7 7 7 7
(integer) 7
# spop key count
127.0.0.1:6379> spop test_set # 省略 count , 读一个
"4"
127.0.0.1:6379> spop test_set 4 # 4代表读多少个值
1) "3"
2) "6"
3) "2"
4) "https://baidu.com"

redis-py

pip install redis

import redis
client = redis.StrictRedis()
client = redis.StrictRedis(host='192.168.1.100', port=6379, password='123456')

client.lpush('c6', 123) # 列表左侧添加一个数字
client.llen('c6') # 长度
value = client.lpop('c6') # 从列表右侧读一个值

client.sadd('test_set', 'www.baidu.com') # 集合里添加一个网址
url = client.spop('test_set') # 读一个值
length=client.scard('test_set') # 集合的长度
import json
json_str = json.dumps(dict, indent=4) # dict -> json, 或 包含字典的列表 -> json
json_dict = json.loads(str) # json -> dict

selenium

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome('chromedriver')
url = "https://www.baidu.com"
driver.get(url)
try:
    # WebDriverWait(driver,
    # 30).until(EC.presence_of_element_located((By.CLASS_NAME,"site-description")))
    # WebDriverWait(driver, 30).\
    #    until(EC.presence_of_element_located((By.CLASS_NAME,
    #                                          "s_btn")))
    # By.ID
    # By.NAME
    # By.XPATH
    WebDriverWait(driver, 30).\ #等30s,直到出现 id为 su的元素
        until(EC.presence_of_element_located((By.ID, 'su')))
except Exception as e:
    print("网页太慢", e)

html = driver.page_source # 获取源码

element = driver.find_element_by_id("su")
element = driver.find_element_by_name("ie")
element_list = driver.find_elements_by_id("su")
element_list = driver.find_elements_by_name("ie")
element = driver.find_element_by_xpath('//input[@id="su"]')
element = driver.find_elements_by_xpath('//input[@id="su"]')
comment = driver.find_element_by_xpath('//*[@id="qrcode"]/div/div[2]/p/b')
print(comment.text) # text 获取文本

driver.quit() # 退出

以上所述就是小编给大家介绍的《python 爬虫开发从入门到实践 读书笔记(二)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Design for Hackers

Design for Hackers

David Kadavy / Wiley / 2011-10-18 / USD 39.99

Discover the techniques behind beautiful design?by deconstructing designs to understand them The term ?hacker? has been redefined to consist of anyone who has an insatiable curiosity as to how thin......一起来看看 《Design for Hackers》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具