我正在尝试提取给定url的纯文本。 根据我的搜索,最相关的工具似乎是beauthulsoup,所以我编写了一个简单的程序来测试。 但是,我发现它仍然不能满足我的要求。结果包含许多非纯文本。在
您可以运行以下python代码来查看结果。在
import urllib
url = "http://www.amfastech.com/2015/07/lenovo-k3-note-brutally-honest-review-specifications-pros-cons.html"
html = urllib.urlopen(url).read().decode('utf8')
from bs4 import BeautifulSoup
raw = BeautifulSoup(html).get_text()
当您看到raw
时,结果包含如下代码:
所以我的问题是,如何真正地用Python从html中获得干净的纯文本呢。我看到很多web工具都支持所谓的book view模式,在这种模式下,你只能在大多数情况下看到主要文章,所以我认为提取干净的纯文本应该不是问题。谢谢!在
您需要提取} 方法销毁其中的内容。从那里只需使用^{} 来获得soup文本。在
style
和script
标记,并使用^{结果是:
好吧,你用beauthoulsoup是错误的,为了提取你的文本,你不应该得到原始文本……BS不是一个神奇的魔杖,它能从一个页面中猜出你需要什么,它需要告诉你该怎么做。因此,您应该查找要提取的对象的类和id:
还有一些清理工作要做(主要是因为文本中的JS广告),但大部分都在那里。您需要查看要保留在主体中的标记/类/标识。在
它没有关联,“原始”文本只是一种不同的CSS样式,只显示文本。但这并没有使页面的来源更简单。在
相关问题 更多 >
编程相关推荐