课堂笔记:
一、BeautifulSoup 解析库
二、MongoDB 存储库
三、requests-html 请求库
BeautifulSoup
一、什么bs4,为何要使用bs4?
是一个基于re开发的解析库,能够提供一些强大的解析功能。
提升提取数据的效率与爬虫开发效率。
二、安装与使用
pip3 install beautifulsoup4 # 安装bs4
pip3 install lxml # 下载lxml解析器
MongoDB 非关系型数据库
一 安装与使用
一、下载安装
https://www.mongodb.com/download-center/community
二、在C盘建立一个data/db文件夹
- 数据的存放路径
三、mongod启动服务
进入终端,输入mongod启动mongoDB服务。
四、mongo进入mongoDB客户端
打开一个新的终端,输入mongo进入客户端
二 数据库操做
数据库操做:
切换库:
SQL:
use admin; 有则切换,无则报错。
MongoDB:
use tank; 有则切换,无则建立,并切换tank库中。
查数据库:
SQL:
show databases;
MongoDB:
show dbs;
显示的数据库若无数据,则不显示。
删除库:
SQL:
drop database
MongoDB:
db.dropDatabase()
集合操做: MySQL中叫作表。
建立集合:
SQL:
create table f1, f2...
MongoDB:
# 在当前库中经过.来建立集合
db.student
插入数据:
# 插入多条数据
db.student.insert([{"name1": "tank1"}, {"name2": "tank2"}])
# 插入一条
db.student.insert({"name": "tank"})
查数据:
# 查找student集合中全部数据
db.student.find({})
# 查一条 查找name为tank的记录
db.student.find({"name":"tank"})
三 python连接MongoDB
一、下载第三方模块pymongo
pip3 install pymongo
二、连接mongoDB客户端
client = MongoClient('localhost', 27017)
做业:
一、整理课堂内容,并写博客
二、基于豌豆荚爬取剩下的简介截图图片地址、网友评论
三、把豌豆荚爬取的数据插入mongoDB中
- 建立一个wandoujia库
- 把主页的数据存放一个名为index集合中
- 把详情页的数据存放一个名为detail集合中
解析库之bs4
1 '''''' 2 ''' 3 pip3 install beautifulsoup4 # 安装bs4 4 pip3 install lxml # 下载lxml解析器 5 ''' 6 html_doc = """ 7 <html><head><title>The Dormouse's story</title></head> 8 <body> 9 <p class="sister"><b>$37</b></p> 10 <p class="story" id="p">Once upon a time there were three little sisters; and their names were 11 <a href="http://example.com/elsie" class="sister" >Elsie</a>, 12 <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and 13 <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>; 14 and they lived at the bottom of a well.</p> 15 16 <p class="story">...</p> 17 """ 18 19 # 从bs4中导入BeautifulSoup 20 from bs4 import BeautifulSoup 21 22 # 调用BeautifulSoup实例化获得一个soup对象 23 # 参数一: 解析文本 24 # 参数二: 25 # 参数二: 解析器(html.parser、lxml...) 26 soup = BeautifulSoup(html_doc, 'lxml') 27 28 print(soup) 29 print('*' * 100) 30 print(type(soup)) 31 print('*' * 100) 32 # 文档美化 33 html = soup.prettify() 34 print(html)
bs4之遍历文档树html
1 html_doc = """<html><head><title>The Dormouse's story</title></head><body><p class="sister"><b>$37</b></p><p class="story" id="p">Once upon a time there were three little sisters; and their names were<b>tank</b><a href="http://example.com/elsie" class="sister" >Elsie</a>,<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;and they lived at the bottom of a well.<hr></hr></p><p class="story">...</p>""" 2 3 from bs4 import BeautifulSoup 4 soup = BeautifulSoup(html_doc, 'lxml') 5 6 ''' 7 遍历文档树: 8 一、直接使用 9 二、获取标签的名称 10 三、获取标签的属性 11 四、获取标签的内容 12 五、嵌套选择 13 六、子节点、子孙节点 14 七、父节点、祖先节点 15 八、兄弟节点 16 ''' 17 18 # 一、直接使用 19 print(soup.p) # 查找第一个p标签 20 print(soup.a) # 查找第一个a标签 21 22 # 二、获取标签的名称 23 print(soup.head.name) # 获取head标签的名称 24 25 # 三、获取标签的属性 26 print(soup.a.attrs) # 获取a标签中的全部属性 27 print(soup.a.attrs['href']) # 获取a标签中的href属性 28 29 # 四、获取标签的内容 30 print(soup.p.text) # $37 31 32 # 五、嵌套选择 33 print(soup.html.head) 34 35 # 六、子节点、子孙节点 36 print(soup.body.children) # body全部子节点,返回的是迭代器对象 37 print(list(soup.body.children)) # 强转成列表类型 38 39 print(soup.body.descendants) # 子孙节点 40 print(list(soup.body.descendants)) # 子孙节点 41 42 # 七、父节点、祖先节点 43 print(soup.p.parent) # 获取p标签的父亲节点 44 # 返回的是生成器对象 45 print(soup.p.parents) # 获取p标签全部的祖先节点 46 print(list(soup.p.parents)) 47 48 # 八、兄弟节点 49 # 找下一个兄弟 50 print(soup.p.next_sibling) 51 # 找下面全部的兄弟,返回的是生成器 52 print(soup.p.next_siblings) 53 print(list(soup.p.next_siblings)) 54 55 # 找上一个兄弟 56 print(soup.a.previous_sibling) # 找到第一个a标签的上一个兄弟节点 57 # 找到a标签上面的全部兄弟节点 58 print(soup.a.previous_siblings) # 返回的是生成器 59 print(list(soup.a.previous_siblings))
bs4之搜索文档树python
1 '''''' 2 html_doc = """<html><head><title>The Dormouse's story</title></head><body><p class="sister"><b>$37</b></p><p class="story" id="p">Once upon a time there were three little sisters; and their names were<b>tank</b><a href="http://example.com/elsie" class="sister" >Elsie</a>,<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;and they lived at the bottom of a well.<hr></hr></p><p class="story">...</p>""" 3 ''' 4 搜索文档树: 5 find() 找一个 6 find_all() 找多个 7 8 标签查找与属性查找: 9 标签: 10 name 属性匹配 11 attrs 属性查找匹配 12 text 文本匹配 13 14 - 字符串过滤器 15 字符串全局匹配 16 17 - 正则过滤器 18 re模块匹配 19 20 - 列表过滤器 21 列表内的数据匹配 22 23 - bool过滤器 24 True匹配 25 26 - 方法过滤器 27 用于一些要的属性以及不须要的属性查找。 28 29 属性: 30 - class_ 31 - id 32 ''' 33 34 from bs4 import BeautifulSoup 35 soup = BeautifulSoup(html_doc, 'lxml') 36 37 # # 字符串过滤器 38 # # name 39 # p_tag = soup.find(name='p') 40 # print(p_tag) # 根据文本p查找某个标签 41 # # 找到全部标签名为p的节点 42 # tag_s1 = soup.find_all(name='p') 43 # print(tag_s1) 44 # 45 # 46 # # attrs 47 # # 查找第一个class为sister的节点 48 # p = soup.find(attrs={"class": "sister"}) 49 # print(p) 50 # # 查找全部class为sister的节点 51 # tag_s2 = soup.find_all(attrs={"class": "sister"}) 52 # print(tag_s2) 53 # 54 # 55 # # text 56 # text = soup.find(text="$37") 57 # print(text) 58 # 59 # 60 # # 配合使用: 61 # # 找到一个id为link二、文本为Lacie的a标签 62 # a_tag = soup.find(name="a", attrs={"id": "link2"}, text="Lacie") 63 # print(a_tag) 64 65 66 67 # # 正则过滤器 68 # import re 69 # # name 70 # p_tag = soup.find(name=re.compile('p')) 71 # print(p_tag) 72 73 # 列表过滤器 74 # import re 75 # # name 76 # tags = soup.find_all(name=['p', 'a', re.compile('html')]) 77 # print(tags) 78 79 # - bool过滤器 80 # True匹配 81 # 找到有id的p标签 82 # p = soup.find(name='p', attrs={"id": True}) 83 # print(p) 84 85 # 方法过滤器 86 # 匹配标签名为a、属性有id没有class的标签 87 # def have_id_class(tag): 88 # if tag.name == 'a' and tag.has_attr('id') and tag.has_attr('class'): 89 # return tag 90 # 91 # tag = soup.find(name=have_id_class) 92 # print(tag)
1 ''' 2 主页: 3 图标地址、下载次数、大小、详情页地址 4 5 详情页: 6 游戏名、图标名、好评率、评论数、小编点评、简介、网友评论、1-5张截图连接地址、下载地址 7 https://www.wandoujia.com/wdjweb/api/category/more?catId=6001&subCatId=0&page=1&ctoken=FRsWKgWBqMBZLdxLaK4iem9B 8 9 https://www.wandoujia.com/wdjweb/api/category/more?catId=6001&subCatId=0&page=2&ctoken=FRsWKgWBqMBZLdxLaK4iem9B 10 11 https://www.wandoujia.com/wdjweb/api/category/more?catId=6001&subCatId=0&page=3&ctoken=FRsWKgWBqMBZLdxLaK4iem9B 12 13 32 14 ''' 15 import requests 16 from bs4 import BeautifulSoup 17 # 一、发送请求 18 def get_page(url): 19 response = requests.get(url) 20 return response 21 22 # 二、开始解析 23 # 解析主页 24 def parse_index(data): 25 soup = BeautifulSoup(data, 'lxml') 26 27 # 获取全部app的li标签 28 app_list = soup.find_all(name='li', attrs={"class": "card"}) 29 for app in app_list: 30 # print('tank *' * 1000) 31 # print(app) 32 # 图标地址 33 img = app.find(name='img').attrs['data-original'] 34 print(img) 35 36 # 下载次数 37 down_num = app.find(name='span', attrs={"class": "install-count"}).text 38 print(down_num) 39 40 import re 41 # 大小 42 size = soup.find(name='span', text=re.compile("\d+MB")).text 43 print(size) 44 45 # 详情页地址 46 detail_url = soup.find(name='a', attrs={"class": "detail-check-btn"}).attrs['href'] 47 print(detail_url) 48 49 50 def main(): 51 for line in range(1, 33): 52 url = f"https://www.wandoujia.com/wdjweb/api/category/more?catId=6001&subCatId=0&page={line}&ctoken=FRsWKgWBqMBZLdxLaK4iem9B" 53 54 # 一、往app接口发送请求 55 response = get_page(url) 56 # print(response.text) 57 print('*' * 1000) 58 # 反序列化为字典 59 data = response.json() 60 # 获取接口中app标签数据 61 app_li = data['data']['content'] 62 # print(app_li) 63 # 二、解析app标签数据 64 parse_index(app_li) 65 66 67 if __name__ == '__main__': 68 main()
1 ''' 2 主页: 3 图标地址、下载次数、大小、详情页地址 4 5 详情页: 6 游戏名、好评率、评论数、小编点评、下载地址、简介、网友评论、1-5张截图连接地址、 7 https://www.wandoujia.com/wdjweb/api/category/more?catId=6001&subCatId=0&page=1&ctoken=FRsWKgWBqMBZLdxLaK4iem9B 8 9 https://www.wandoujia.com/wdjweb/api/category/more?catId=6001&subCatId=0&page=2&ctoken=FRsWKgWBqMBZLdxLaK4iem9B 10 11 https://www.wandoujia.com/wdjweb/api/category/more?catId=6001&subCatId=0&page=3&ctoken=FRsWKgWBqMBZLdxLaK4iem9B 12 13 32 14 ''' 15 import requests 16 from bs4 import BeautifulSoup 17 # 一、发送请求 18 def get_page(url): 19 response = requests.get(url) 20 return response 21 22 # 二、开始解析 23 # 解析详情页 24 def parse_detail(text): 25 soup = BeautifulSoup(text, 'lxml') 26 # print(soup) 27 28 # app名称 29 name = soup.find(name="span", attrs={"class": "title"}).text 30 # print(name) 31 32 # 好评率 33 love = soup.find(name='span', attrs={"class": "love"}).text 34 # print(love) 35 36 # 评论数 37 commit_num = soup.find(name='a', attrs={"class": "comment-open"}).text 38 # print(commit_num) 39 40 # 小编点评 41 commit_content = soup.find(name='div', attrs={"class": "con"}).text 42 # print(commit_content) 43 44 # app下载连接 45 download_url = soup.find(name='a', attrs={"class": "normal-dl-btn"}).attrs['href'] 46 # print(download_url) 47 48 print( 49 f''' 50 ============= tank ============== 51 app名称:{name} 52 好评率: {love} 53 评论数: {commit_num} 54 小编点评: {commit_content} 55 app下载连接: {download_url} 56 ============= end ============== 57 ''' 58 ) 59 60 61 62 # 解析主页 63 def parse_index(data): 64 soup = BeautifulSoup(data, 'lxml') 65 66 # 获取全部app的li标签 67 app_list = soup.find_all(name='li', attrs={"class": "card"}) 68 for app in app_list: 69 # print(app) 70 # print('tank' * 1000) 71 # print('tank *' * 1000) 72 # print(app) 73 # 图标地址 74 # 获取第一个img标签中的data-original属性 75 img = app.find(name='img').attrs['data-original'] 76 print(img) 77 78 # 下载次数 79 # 获取class为install-count的span标签中的文本 80 down_num = app.find(name='span', attrs={"class": "install-count"}).text 81 print(down_num) 82 83 import re 84 # 大小 85 # 根据文本正则获取到文本中包含 数字 + MB(\d+表明数字)的span标签中的文本 86 size = soup.find(name='span', text=re.compile("\d+MB")).text 87 print(size) 88 89 # 详情页地址 90 # 获取class为detail-check-btn的a标签中的href属性 91 # detail_url = soup.find(name='a', attrs={"class": "name"}).attrs['href'] 92 # print(detail_url) 93 94 # 详情页地址 95 detail_url = app.find(name='a').attrs['href'] 96 print(detail_url) 97 98 # 三、往app详情页发送请求 99 response = get_page(detail_url) 100 101 # 四、解析app详情页 102 parse_detail(response.text) 103 104 105 def main(): 106 for line in range(1, 33): 107 url = f"https://www.wandoujia.com/wdjweb/api/category/more?catId=6001&subCatId=0&page={line}&ctoken=FRsWKgWBqMBZLdxLaK4iem9B" 108 109 # 一、往app接口发送请求 110 response = get_page(url) 111 # print(response.text) 112 print('*' * 1000) 113 # 反序列化为字典 114 data = response.json() 115 116 # 获取接口中app标签数据 117 app_li = data['data']['content'] 118 # print(app_li) 119 # 二、解析app标签数据 120 parse_index(app_li) 121 122 123 if __name__ == '__main__': 124 main()
1 from pymongo import MongoClient 2 3 # 一、连接mongoDB客户端 4 # 参数1: mongoDB的ip地址 5 # 参数2: mongoDB的端口号 默认:27017 6 client = MongoClient('localhost', 27017) 7 # print(client) 8 9 # 二、进入tank_db库,没有则建立 10 # print(client['tank_db']) 11 12 # 三、建立集合 13 # print(client['tank_db']['people']) 14 15 # 四、给tank_db库插入数据 16 17 # 1.插入一条 18 # data1 = { 19 # 'name': 'tank', 20 # 'age': 18, 21 # 'sex': 'male' 22 # } 23 # client['tank_db']['people'].insert(data1) 24 25 # 2.插入多条 26 # data1 = { 27 # 'name': 'tank', 28 # 'age': 18, 29 # 'sex': 'male' 30 # } 31 # data2 = { 32 # 'name': '戚志云', 33 # 'age': 84, 34 # 'sex': 'female' 35 # } 36 # data3 = { 37 # 'name': '沈金金', 38 # 'age': 73, 39 # 'sex': 'male' 40 # } 41 # client['tank_db']['people'].insert([data1, data2, data3]) 42 # 43 # # 五、查数据 44 # # 查看全部数据 45 # data_s = client['tank_db']['people'].find() 46 # print(data_s) # <pymongo.cursor.Cursor object at 0x000002EEA6720128> 47 # # 须要循环打印全部数据 48 # for data in data_s: 49 # print(data) 50 # 51 # # 查看一条数据 52 # data = client['tank_db']['people'].find_one() 53 # print(data) 54 55 # 官方推荐使用 56 # 插入一条insert_one 57 # client['tank_db']['people'].insert_one() 58 # 插入多条insert_many 59 # client['tank_db']['people'].insert_many()