BeautifulSoup是一个模块,该模块用于接收一个HTML或XML字符串,然后将其进行格式化,之后遍可以使用他提供的方法进行快速查找指定元素,从而使得在HTML或XML中查找指定元素变得简单。

举个简单的例子对其进行运用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from django.test import TestCase
 
# Create your tests here.
 
from bs4 import BeautifulSoup
 
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
asdf
    <div class="title">
        <b>The Dormouse's story总共</b>
        <h1>f</h1>
    </div>
<div class="story">Once upon a time there were three little sisters; and their names were
    <a  class="sister0" >Els<span>f</span>ie</a>,
    <a href="http://example.com/lacie" class="sister" >Lacie</a> and
    <a href="http://example.com/tillie" class="sister" >Tillie</a>;
and they lived at the bottom of a well.</div>
ad<br/>sf
<p class="story">...</p>
</body>
</html>
"""
 
soup = BeautifulSoup(html_doc, features="lxml")
# 找到第一个a标签
# tag1 = soup.find(name='a')
 
# 循环所有标签
from bs4.element import Tag
from bs4.element import NavigableString
for tag in soup.body.descendants:
    if isinstance(tag,Tag):
        # print(tag.name,tag.attrs)
        pass
 
# 内容
# tag1 = soup.find(name='a')
# tag1.clear()
# print(soup)
 
# 属性
tag1 = soup.find(name='a')
del tag1.attrs['class']
print(soup)

常用参数介绍  

name,标签名称

1 tag = soup.find('a')
2 name = tag.name # 获取该标签的名字
3 print(name)
4 tag.name = 'span' # 设置
5 print(soup)
View Code

attr,标签属性

1 tag = soup.find('a')
2 attrs = tag.attrs # 获取
3 print(attrs)
4 tag.attrs = {'k':1}
5 tag.attrs['id'] = '22'
6 print(soup)
View Code

 children,所有子标签

1 body = soup.find('body')
2 v = body.children
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-30
  • 2022-12-23
  • 2021-11-05
  • 2021-12-24
  • 2022-01-14
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-03-09
  • 2022-12-23
  • 2021-09-18
  • 2022-12-23
  • 2021-08-01
  • 2022-12-23
相关资源
相似解决方案