【发布时间】:2014-06-03 02:53:06
【问题描述】:
我希望在呈现 HTML 之后让网站上的所有文本都可见。我正在使用带有 Scrapy 框架的 Python 工作。
使用xpath('//body//text()') 我可以得到它,但是使用 HTML 标签,我只想要文本。有什么解决办法吗?
【问题讨论】:
标签: python html xpath web-scraping scrapy
我希望在呈现 HTML 之后让网站上的所有文本都可见。我正在使用带有 Scrapy 框架的 Python 工作。
使用xpath('//body//text()') 我可以得到它,但是使用 HTML 标签,我只想要文本。有什么解决办法吗?
【问题讨论】:
标签: python html xpath web-scraping scrapy
xpath('//body//text()') 并不总是将 dipper 驱动到您上次使用的标签中的节点(在您的案例正文中)。如果您键入 xpath('//body/node()/text()').extract(),您将看到您的 html 正文中的节点。你可以试试xpath('//body/descendant::text()')。
【讨论】:
最简单的选择是 extract //body//text() 和 join 找到所有内容:
''.join(sel.select("//body//text()").extract()).strip()
其中sel 是Selector 实例。
另一种选择是使用nltk的clean_html():
>>> import nltk
>>> html = """
... <div class="post-text" itemprop="description">
...
... <p>I would like to have all the text visible from a website, after the HTML is rendered. I'm working in Python with Scrapy framework.
... With <code>xpath('//body//text()')</code> I'm able to get it, but with the HTML tags, and I only want the text. Any solution for this? Thanks !</p>
...
... </div>"""
>>> nltk.clean_html(html)
"I would like to have all the text visible from a website, after the HTML is rendered. I'm working in Python with Scrapy framework.\nWith xpath('//body//text()') I'm able to get it, but with the HTML tags, and I only want the text. Any solution for this? Thanks !"
另一种选择是使用BeautifulSoup的get_text():
get_text()如果您只想要文档或标签的文本部分,您可以 可以使用
get_text()方法。它返回文档中的所有文本 或在标签下方,作为单个 Unicode 字符串。
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(html)
>>> print soup.get_text().strip()
I would like to have all the text visible from a website, after the HTML is rendered. I'm working in Python with Scrapy framework.
With xpath('//body//text()') I'm able to get it, but with the HTML tags, and I only want the text. Any solution for this? Thanks !
另一种选择是使用lxml.html的text_content():
.text_content()返回元素的文本内容,包括 其子项的文本内容,没有标记。
>>> import lxml.html
>>> tree = lxml.html.fromstring(html)
>>> print tree.text_content().strip()
I would like to have all the text visible from a website, after the HTML is rendered. I'm working in Python with Scrapy framework.
With xpath('//body//text()') I'm able to get it, but with the HTML tags, and I only want the text. Any solution for this? Thanks !
【讨论】:
nltk 弃用了他们的 clean_html 方法,而是推荐:NotImplementedError: To remove HTML markup, use BeautifulSoup's get_text() function
你试过了吗?
xpath('//body//text()').re('(\w+)')
或
xpath('//body//text()').extract()
【讨论】: