[
用python
提取文本,您的目标是从python中的html文件中提取文本editor。正则表达式不够强大,对于形成较差的html。虽然通常建议使用美丽的汤,但它可以拾取不需要的内容,例如JavaScript,并且无法解释HTML实体。有希望的替代方案:html2text
Optimal Code for Text ExtractionThe code below offers an effective solution that filters out unwanted elements and preserves HTML entities:
from urllib.request import urlopen 来自BS4进口美丽的小组 url =“ http://news.bbc.co.uk/2/hi/health/2284783.stm” html = urlopen(url).Read() 汤= beautifutsoup(html,features =“ html.parser”) #删除脚本和样式 对于汤中的脚本([“脚本”,“样式”)): script.extract() #提取文字 text = soup.get_text() #转换线路休息并删除空格 lines =(line.strip()for text.splitlines())中的行 chunks =(thrase.strip()用于行中的行中的行中的线条。 text ='\ n'.join(如果块,则在块中的块) print(text)
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3