第1关:文本提取入门 ⭐

📋 任务目标

这是最简单的一关!请提取下面的标题和段落文本。

💡 提示

使用 BeautifulSoup 的 find()find_all() 方法

示例代码:

from bs4 import BeautifulSoup
import requests

url = 'https://req.haleibc.com/level1'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')

# 提取标题
title = soup.find('h2', class_='main-title').text
print(title)

# 提取所有段落
paragraphs = soup.find_all('p', class_='content')
for p in paragraphs:
    print(p.text)

欢迎来到Python爬虫的世界!

Python是一门非常适合做网络爬虫的编程语言。

通过学习爬虫,你可以自动化地收集互联网上的数据。

让我们从最简单的文本提取开始吧!

✅ 期望结果

标题:欢迎来到Python爬虫的世界!

段落1:Python是一门非常适合做网络爬虫的编程语言。

段落2:通过学习爬虫,你可以自动化地收集互联网上的数据。

段落3:让我们从最简单的文本提取开始吧!

返回首页 下一关 →