网站首页 > 开源技术 正文
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
猜你喜欢
- 2025-06-04 测试进阶:实现跨请求地保持登录的神器session你get了么?
- 2025-06-04 Requestium一个比Requests跟实用的python库
- 2025-06-04 Python模拟登入的N种方式,总有一种方式适合你(建议收藏)
- 2025-06-04 RobotFramework之http接口测试库RequestsLibrary使用介绍(十)
- 2025-06-04 Kafka Producer 参数 max.in.flight.requests.per.connection 配置
- 2025-06-04 如何搭建接口自动化测试框架?(接口自动化测试框架设计)
- 2025-06-04 python编程实践:网络爬虫,从学习Requests 模块开始
- 2025-06-04 一文详解 requests 库中 json 参数和 data 参数的用法
- 2025-06-04 Python爬虫5 requests库get、post的使用
- 2025-06-04 Python-实战-requests库标准入门姿势
你 发表评论:
欢迎- 最近发表
-
- 后端服务太慢?试试这 7 招(后端 服务端 区别)
- 做一个适合二次开发的低代码平台,把程序员从curd中解脱出来-1
- Caffeine缓存 最快缓存 内存缓存(caffeine缓存使用)
- Java性能优化的10大策略(java性能调优从哪几个方面入手)
- New Balance M576PGT 全新配色设计
- x-cmd pkg | qrencode - 二维码生成工具
- 平和精英抽奖概率是多少 平和精英抽奖物品一览
- x-cmd pkg | tmux - 开源终端多路复用器(terminal multiplexer)
- 漫威官方App中文版上线:全站漫画限时免费
- macOS Monterey 12.7.4 (21H1123) 正式版发布,ISO、IPSW、PKG 下载
- 标签列表
-
- jdk (81)
- putty (66)
- rufus (78)
- 内网穿透 (89)
- okhttp (70)
- powertoys (74)
- windowsterminal (81)
- netcat (65)
- ghostscript (65)
- veracrypt (65)
- asp.netcore (70)
- wrk (67)
- aspose.words (80)
- itk (80)
- ajaxfileupload.js (66)
- sqlhelper (67)
- express.js (67)
- phpmailer (67)
- xjar (70)
- redisclient (78)
- wakeonlan (66)
- tinygo (85)
- startbbs (72)
- webftp (82)
- vsvim (79)
本文暂时没有评论,来添加一个吧(●'◡'●)