BeautifulSoup4模块与lxml解析器使用
-
爬虫解析库之bs4模块
-
爬取红牛分公司数据

详细
-
爬虫解析库之bs4模块
# Beautiful Soup4 1.是一个可以从HTML或XML文件中提取数据的Python库 2.通过特定的解析器实现惯用的文档导航,查找,修改文档的方式

-
bs4模块的基本使用
# 构造一个测试用的网页数据 from bs4 import BeautifulSoup htmlDoc = """ <html> <head> <title>The Dormouse's story</title> </head> <body> <p class="title"> <b>The Dormouse's story</b> </p> <p class="story">Once upon a time there were three little sisters; and their names were <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a> <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a> and they lived at the bottom of a well.</p> <p class="story">...</p> </body> </html> """ # 使用方法: 构造一个bs4解析器对象 soup = BeautifulSoup(htmlDoc, 'lxml')

必须要掌握的方法
1.soup.标签名 返回从上往下第一个该标签
eg:soup.a

2.soup.标签名.text 返回标签内部的文本,包含后代所有的文本
eg:soup.p.text

3.soup.a.attrs 返回标签内部所有属性(字典的数据类型)
eg:soup.a.attrs

4.soup.标签名.attrs.get('属性名') 也可简写为 soup.标签名.get('属性名') 返回某标签的指定属性值 eg: soup.a.attrs.get('href') soup.a.get('href')

了解即可的方法
1.soup.标签名.children 返回标签内部所有的子标签,是个迭代器需要循环取值 eg:soup.p.children for i in soup.p.children: print(i)

2.soup.标签名.contents
返回标签内部所有元素(列表的数据类型,包含换行符等)
eg:soup.p.contents

3.soup.标签名.parent 返回该标签的父标签
soup.标签名.parents 返回该标签的所有祖先标签
eg:soup.p.parent
soup.p.parents


1.find(条件) 查找符合条件的第一个标签对象 eg: soup.find(name='a') soup.find(name='a', id='link2')

2.避免冲突:冲突的关键字可加下划线区分,也可作为键值对放入attrs参数中 soup.find(冲突参数_='') soup.find(attrs={'冲突参数':''}) eg: soup.find(name='p',class_='title') soup.find(name='p',attrs={'class':'title'}) # class属性查找属于成员运算,不加name参数查找该类别下所有标签 soup.find(attrs={'class':'title'})

3.find_all() 查找所有符合条件的标签(列表的数据类型) soup.find_all(条件) eg: soup.find_all('a') # name参数可省,其他使用方法一致

4.select(参数) 参数是css选择器,返回列表 css选择器 (1)标签选择器 标签名 (2)id选择器 #id名 id='id' eg:#d1 等价于id='d1' (3)class选择器 .class名 class='class名' eg:.c1 等价于class='c1' (4)子标签选择器 标签名>子标签名 查找标签内部所有的该子标签 eg:div>p (5)后代标签选择器 标签名 后代标签名 查找标签内部所有的该后代标签 eg:div p 使用案例: 1.soup.select('.title') 查找class含有title的标签 2.soup.select('.title b') 查找class含有title的标签的所有后代b标签 3.soup.select('#link1') 查找id等于link1的标签 4.soup.select('#link1 sqan') 查找id等于link1的标签的所有后代span标签 5.soup.select('#list-2 .element') 查找id等于list-2的标签的所有class为element的后代标签 6.soup.select('#list-2')[0].select('.element') # select可以连续使用 很少用 查找id为list-2的第一个标签的所有class为element的后代标签

-
爬取红牛分公司数据
需求:获取红牛所有分公司详细数据(名称、地址、邮箱、电话)
1、查找数据加载方式 得知是直接加载的
2、朝该网页发送请求获取页面数据之后筛选即可
import requests from bs4 import BeautifulSoup 数据就在页面上显示,直接发送请求即可 res = requests.get('http://www.redbull.com.cn/about/branch') soup = BeautifulSoup(res.text, 'lxml') # 1.获取分公司名称 h2List = soup.find_all('h2') nameList = [tag.text for tag in h2List] # 2.获取分公司地址 p1List = soup.find_all(name='p', attrs={'class': 'mapIco'}) addrList = [tag.text for tag in p1List] # 3.获取分公司邮编 p2List = soup.find_all(name='p', attrs={'class': 'mailIco'}) postalCodeList = [tag.text for tag in p2List] # 4.获取分公司电话 p3List = soup.find_all(name='p', attrs={'class': 'telIco'}) phoneList = [tag.text for tag in p3List] # 5.将获取到的分公司数据一一对应(按照名称循环) for i in range(len(nameList)): print(""" 公司名称:{} 公司地址:{} 公司邮箱:{} 公司电话:{} """.format(nameList[i], addrList[i], postalCodeList[i], phoneList[i]) )

浙公网安备 33010602011771号