爬虫笔记15:bs4中的select()方法、修改文档树

一、select()方法
我们可以通过css选择器的方式来提取数据。但是需要注意的是这里面需要我们掌握css语法。

select()返回的是列表形式。

1、常用的几个查找方式:

from bs4 import BeautifulSoup

html_doc = """
The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were Elsie, Lacie and Tillie; and they lived at the bottom of a well.

...

""" soup = BeautifulSoup(html_doc,"lxml") # 1 找a标签 print(soup.select('a')) # 通过标签的名称查找 # 2 通过类名来查找:class="sister" print(soup.select('.sister')) # 3 通过id查找:id="link1" print(soup.select('#link1')) # 4 特殊的查找方式:选择父元素是 的所有 < title> 元素。注意是'head > title',不是'head' > title' print(soup.select('head > 'title')) # 5 获取文本内容 print(soup.select('title')[0].string) print(soup.select('title')[0].get_text())

结果:
爬虫笔记15:bs4中的select()方法、修改文档树_第1张图片
更加详细的介绍,可以参考:https://www.w3school.com.cn/cssref/css_selectors.asp
实际上掌握以上几个就够用了。

2、获取所有class=even的tr标签

trs = soup.select('.even')
print(trs)

或者:

trs = soup.select('tr[class="even"]')
print(trs)

3、stripped_strings返回的是一个generator生成器,通过list()显示出来。

from bs4 import BeautifulSoup

html = """
职位名称 职位类别 人数 地点 发布时间
22989-金融云区块链高级研发工程师(深圳) 技术类 1 深圳 2017-11-25
22989-金融云高级后台开发 技术类 2 深圳 2017-11-25
SNG16-腾讯音乐运营开发工程师(深圳) 技术类 2 深圳 2017-11-25
SNG16-腾讯音乐业务运维工程师(深圳) 技术类 1 深圳 2017-11-25
TEG03-高级研发工程师(深圳) 技术类 1 深圳 2017-11-24
TEG03-高级图像算法研发工程师(深圳) 技术类 1 深圳 2017-11-24
TEG11-高级AI开发工程师(深圳) 技术类 4 深圳 2017-11-24
15851-后台开发工程师 技术类 1 深圳 2017-11-24
15851-后台开发工程师 技术类 1 深圳 2017-11-24
SNG11-高级业务运维工程师(深圳) 技术类 1 深圳 2017-11-24
""" soup = BeautifulSoup(html,"lxml") trs = soup.select('tr')[1:] #print(trs) tr=trs[0] print(tr) print(type(tr.stripped_strings)) print(list(tr.stripped_strings)) #stripped_strings返回的是一个generator生成器,通过list()显示出来

结果:
爬虫笔记15:bs4中的select()方法、修改文档树_第2张图片
结合for循环:
爬虫笔记15:bs4中的select()方法、修改文档树_第3张图片

二、修改文档树
• 修改tag的名称和属性
• 修改string:属性赋值,就相当于用当前的内容替代了原来的内容
• append() 向tag中添加内容,就好像Python的列表的 .append() 方法
• decompose() 删除段落,对于一些没有必要的文章段落我们可以给他删除掉

1、修改tag的名称和属性
爬虫笔记15:bs4中的select()方法、修改文档树_第4张图片
2、修改string:属性赋值,就相当于用当前的内容替代了原来的内容
爬虫笔记15:bs4中的select()方法、修改文档树_第5张图片
3、 append() 向tag中添加内容,就好像Python的列表的 .append() 方法。

from bs4 import BeautifulSoup

html_doc = """
The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were Elsie, Lacie and Tillie; and they lived at the bottom of a well.

...

""" soup = BeautifulSoup(html_doc,"lxml") tap_p = soup.p print(tap_p) #print(tap_p.string) tap_p.string = 'you need python' print(tap_p) tap_p.append('123') print(tap_p)

结果:
在这里插入图片描述
4、decompose() 删除段落
爬虫笔记15:bs4中的select()方法、修改文档树_第6张图片

你可能感兴趣的:(爬虫笔记15:bs4中的select()方法、修改文档树)