gpt4 book ai didi

python - 如何使用 Scrapy 从网站获取所有纯文本?

转载 作者:技术小花猫 更新时间:2023-10-29 12:22:58 30 4
gpt4 key购买 nike

在呈现 HTML 后,我希望网站上的所有文本都可见。我在使用 Scrapy 框架的 Python 中工作。使用 xpath('//body//text()') 我可以得到它,但是使用 HTML 标签,我只想要文本。有什么解决办法吗?

最佳答案

最简单的选择是 extract //body//text()join找到的一切:

''.join(sel.select("//body//text()").extract()).strip()

其中 selSelector实例。

另一种选择是使用 nltkclean_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 !"

另一种选择是使用 BeautifulSoupget_text():

get_text()

If you only want the text part of a document or tag, you can use the get_text() method. It returns all the text in a document or beneath a tag, as a single Unicode string.

>>> 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.htmltext_content():

.text_content()

Returns the text content of the element, including the text content of its children, with no markup.

>>> 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 !

关于python - 如何使用 Scrapy 从网站获取所有纯文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23156780/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com