python文本提取,使用Python从HTML文件中提取文本

本文介绍了一种使用Python从HTML文件中提取纯文本的方法。作者希望得到的效果类似于将浏览器中的内容复制并粘贴到记事本中。文章讨论了BeautifulSoup的一些限制,并提出html2text作为更优选项。html2text能正确处理HTML实体并忽略JavaScript,但输出的是Markdown格式,需要额外步骤转换为纯文本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I'd like to extract the text from an HTML file using Python. I want essentially the same output I would get if I copied the text from a browser and pasted it into notepad.

I'd like something more robust than using regular expressions that may fail on poorly formed HTML. I've seen many people recommend Beautiful Soup, but I've had a few problems using it. For one, it picked up unwanted text, such as JavaScript source. Also, it did not interpret HTML entities. For example, I would expect ' in HTML source to be converted to an apostrophe in text, just as if I'd pasted the browser content into notepad.

Update html2text looks promising. It handles HTML entities correctly and ignores JavaScript. However, it does not exactly produce plain text; it produces markdown that would then have to be turned into plain text. It comes with no examples or documentation, but the code looks clean.

Related questions:

解决方案

html2text is a Python program that does a pretty good job at this.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值