python用requests BeautifulSoup下载网页到txt并去掉html标记

python用requests BeautifulSoup下载网页到txt并去掉html标记

技术教程gslnedu2025-05-03 13:10:387A+A-

python用requests BeautifulSoup下载网页到txt并去掉html标记

import requests
from bs4 import BeautifulSoup

url = "https://www.5a8.com"
filename = "www5a8com.txt"

try:
    response = requests.get(url, timeout=10)
    response.raise_for_status()

    # 自动检测编码
    response.encoding = response.apparent_encoding

    # 使用 BeautifulSoup 提取纯文本
    soup = BeautifulSoup(response.text, "html.parser")
    visible_text = soup.get_text(separator="\n", strip=True)  # 用换行符分隔内容

    # 保存处理后的文本
    with open(filename, "w", encoding="utf-8") as f:
        f.write(visible_text)
    print(f"已提取可见文本至 {filename}")

except requests.exceptions.RequestException as e:
    print(f"下载失败: {e}")
except Exception as e:
    print(f"处理过程中发生错误: {e}")

运到方法

D:\code\python\get>python geturl1.py
已提取可见文本至 www5a8com.txt
点击这里复制本文地址 以上内容由朽木教程网整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
qrcode

朽木教程网 © All Rights Reserved.  蜀ICP备2024111239号-8