直接上代码
import requests
from bs4 import BeautifulSoup
# 输入网页地址
url = input("请输入网页地址:")
# 发送请求并获取响应
response = requests.get(url)
# 解析HTML内容
soup = BeautifulSoup(response.content, "html.parser")
# 提取文本内容并保持原来的段落格式
text = ''
for paragraph in soup.find_all('p'):
text += paragraph.get_text() + '\n\n'
# 保存为本地txt文件
with open("output.txt", "w", encoding="utf-8") as f:
f.write(text)
print("已保存为本地txt文件:output.txt")