Changing/Reading 指定的内容 class 使用 BeautifulSoup (BS4)

Changing/Reading the content of a specified class using BeautifulSoup (BS4)

我想更改内容'Hey',在<p class = "test-message-one"> Hey </p>下面:

HTML:

<section id="about" class="about-section">
    <div class="container">
        <div class="row">
            <div class="col-lg-12">
                <h1>About Section</h1>

                <p class = "test-message-one"> Hey </p>

            </div>
        </div>
    </div>
</section>

这是我使用 BS4 库的 Python 代码:

#Manipulating HTML

site_html = open(r"C:\Users\rbaden\desktop\KPI_Site\index.html")


from bs4 import BeautifulSoup

soup =BeautifulSoup(site_html)
#print(soup.prettify())

check_test_content = soup.findAll('tr', { "class" : "test-message-one" })

print(check_test_content)

这是输出:

[]

为什么我只收到 [] 而没有任何内容?

通过 class 搜索您的 HTML 是一种不好的做法吗?

如何读取和更改指定class的内容?

您正在搜索错误的标签。您正在搜索 <tr class="test-message-one"> 元素;标签名称也很重要!

搜索正确的标签名称或完全省略名称:

check_test_content = soup.findAll('p', {"class": "test-message-one"})

或者如果您省略名称,请确保 name 第二个参数,它被称为 attrs:

check_test_content = soup.findAll(attrs={"class": "test-message-one"})

请注意 Element.findAll() 名称已 弃用 ;改用新的 PEP-8 兼容版本;你也可以在这里使用 class_ 关键字参数而不是传入字典:

check_test_content = soup.find_all(class_='test-message-one')

第一个位置参数始终是标签名称,需要关键字参数或字典来搜索 class 属性之类的属性(但因为 class 是保留关键字,您需要拼写它有一个额外的下划线,所以 class_='...').

请参阅文档中的 Searching the tree

演示:

>>> from bs4 import BeautifulSoup
>>> sample = '''\
... <section id="about" class="about-section">
...     <div class="container">
...         <div class="row">
...             <div class="col-lg-12">
...                 <h1>About Section</h1>
... 
...                 <p class = "test-message-one"> Hey </p>
... 
...             </div>
...         </div>
...     </div>
... </section>
... '''
>>> soup = BeautifulSoup(sample)
>>> soup.find_all(class_='test-message-one')
[<p class="test-message-one"> Hey </p>]
>>> soup.findAll('p', {"class": "test-message-one"})
[<p class="test-message-one"> Hey </p>]
>>> soup.findAll(attrs={"class": "test-message-one"})
[<p class="test-message-one"> Hey </p>]