python使用beautifulsoup解析网页,按class样式名搜索元素

在使用BeautifulSoup解析网页时,可以使用`find_all()`方法按照class样式名搜索元素。下面是一个使用示例:

```python
from bs4 import BeautifulSoup

# 假设html是你要解析的网页内容
html = """
<html>
    <body>
        <div class="container">
            <h1>Title</h1>
            <p class="para">Paragraph 1</p>
            <p class="para">Paragraph 2</p>
        </div>
    </body>
</html>
"""

# 创建BeautifulSoup对象
soup = BeautifulSoup(html, 'html.parser')

# 搜索class为"para"的元素
elements = soup.find_all(class_="para")

# 遍历搜索结果
for element in elements:
    print(element.text)
```

运行以上代码,输出结果将是:

```
Paragraph 1
Paragraph 2
```

希望对你有帮助!

相关代码参考