BeautifulSoup
html = """
<tbody>
<tr class="h">
<td class="l" width="374">职位名称</td>
<td>职位类别</td>
<td>人数</td>
<td>地点</td>
<td>发布时间</td>
</tr>
<tr class="even">
<td class="l square"><a target="_blank" href="position_detail.php?id=44569&keywords=Python&tid=0&lid=0">MIG16-基础架构工程师(北京)</a></td>
<td>技术类</td>
<td>1</td>
<td>北京</td>
<td>2018-09-29</td>
</tr>
<tr class="odd">
<td class="l square"><a target="_blank" href="position_detail.php?id=44570&keywords=Python&tid=0&lid=0">MIG16-数据系统高级开发工程师</a></td>
<td>技术类</td>
<td>1</td>
<td>北京</td>
<td>2018-09-29</td>
</tr>
<tr class="even">
<td class="l square"><a target="_blank" href="position_detail.php?id=44567&keywords=Python&tid=0&lid=0">MIG16-基础架构工程师(北京)</a></td>
<td>技术类</td>
<td>1</td>
<td>北京</td>
<td>2018-09-29</td>
</tr>
<tr class="odd">
<td class="l square"><a target="_blank" href="position_detail.php?id=44559&keywords=Python&tid=0&lid=0">18796-专项技术测试(深圳)</a><span class="hot"> </span></td>
<td>技术类</td>
<td>2</td>
<td>深圳</td>
<td>2018-09-29</td>
</tr>
</tbody>
"""
-
导入BeautifulSoup
from bs4 import BeautifulSoup -
转至对象,括号可以加入url,要爬取得模板名,还可以加入‘lxml’解析器
bs = BeautifulSoup(html,‘lxml’)
- **prettify():**格式化输出网页
bs.prettify()
- **prettify():**格式化输出网页
-
取出文本方法:
- 获取全部文本get_text()
t = bs.select(‘a’)
for i in t:
print(i.get_text()) - **string:**同样还有一个方法索引出文本
t = bs.select(‘a’)
for i in t:
print(i.string)
- 获取全部文本get_text()
-
find_all()方法:
- **find_all:**匹配所有网页tr标签,不加过滤条件,获取全部的tr标签
bs.find_all(‘标签名’) - limit限制符合条件前n个标签
bs.find_all(‘tr’,limit=2)- **索引:**相同概念
bs.find_all(‘tr’)[0]
- **索引:**相同概念
- 指定取出所有tr子节点class 传入class是class_ = ‘名’
bs.find_all(‘tr’,class_ = ‘h’) - 选择class=even,同时id=feng的tr子节点
bs.find_all(‘tr’,class_ = ‘even’,id = ‘feng’) - **get_text()**方法,这个方法获取到tag中包含的所有文版内容包括子孙tag中的内容
t = bs.find_all(‘tr’)[2] 必须索引否则报错AttributeError 可以指定索引某个
print(t.get_text()) - 可以传入一个参数作为分隔符,让获取的字符串更好的显示出来
t = bs.find_all(‘tr’)[2]
print(t.get_text(’=’))
还可以加入一个参数strip=True删除返回的字符串左右两边的空格
t = bs.find_all(‘tr’)[2]
print(t.get_text(’=====’,strip = True)) - 想要获取节点的属性,,想获取它的href属性值,或者对于其他的节点元素,我们想要获取name、class、id等属性值的
当输入错误标签里面子节点时会报错KeyError: ‘traget’
t = bs.find_all(‘a’)
for i in t:
print(i[‘target’])
- **find_all:**匹配所有网页tr标签,不加过滤条件,获取全部的tr标签
-
find方法:
- find()方法只选取符合条件的第一个标签,取别的标签报错KeyError索引出错
bs.find(‘tr’) - **contents属性:**该属性返回的是某个节点下的全部子元素,包括子元素的标签名和文本内容。返回的数据类型是列表
t = bs.find(‘tr’)
print(t.contents)列表
可以迭代出
for i in t.contents:
print(i.string) - **children属性:**和contents属性的用法是一样的,但是返回的数据类型是迭代器
t = bs.find(‘tr’)
返回包含空格,
for i in t.children:
print(i.string)
- find()方法只选取符合条件的第一个标签,取别的标签报错KeyError索引出错
-
select方法:
- select 可以通过标签名,标签的class、标签的id,通过标签的name、href等属性来选择我们的元素。使用该方法返回的是一个迭代器,我们可以通过for…in…循环遍历
- 可以连着取标签下的子节点,然后用for迭代出i 然后用i所以用i索引出想要的href
t = bs.select(‘td a’)
for i in t:
print(i[‘href’]) - 通过属性来查找标签,比如查找href属性等于index.html的a节点
a = bs.select(“a[href=‘index.html’]”) - 选择div中的直接子元素img
img = bs.select(“div > img”)
-
strings属性:
-
一个节点只包含一个文本节点,或者是只包含一个节点,strings,获取该文本节点的文本内容,或者是这个节点的文本内容’’‘可能或出现换行和空格等空白文本’’’
o = r.select(‘div’)[1] -
必须索引否则报错’’‘AttributeError: ‘list’ object has no attribute ‘strings’’’’
for i in o.strings:
print(i)
-
-
如果不想获取换行和空格,那么我们可以使用stripped_strings属性
o = r.select(‘div’)[0]
for i in o.stripped_strings:
print(i)