BeautifulSoup库使用实例
时间: 2024-10-20 22:01:26 浏览: 17
Python爬虫库BeautifulSoup的介绍与简单使用实例
BeautifulSoup是一个Python库,用于解析HTML和XML文档,使得处理网页数据变得更加简单。它允许开发者通过标签名、属性或CSS选择器来导航和搜索网页内容。下面是一个简单的例子:
```python
from bs4 import BeautifulSoup
import requests
# 获取网页内容
url = 'https://example.com'
response = requests.get(url)
html_content = response.text
# 使用BeautifulSoup解析HTML
soup = BeautifulSoup(html_content, 'html.parser')
# 找到所有<h1>标签
headings = soup.find_all('h1')
for heading in headings:
print(heading.text)
# 或者找到class为'my-class'的第一个段落
first_paragraph = soup.find('p', class_='my-class')
print(first_paragraph.get_text())
阅读全文