你好!这是一段使用 Python 爬取网页标题和文本,并保存到 txt 文件的代码:
import requestsfrom bs4 import BeautifulSoup
# 请求网页
url = 'http://www.example.com'
response = requests.get(url)
# 解析网页
soup = BeautifulSoup(response.text, 'html.parser')
# 提取标题和文本
title = soup.title.string
text = soup.get_text()
# 保存到 txt 文件