栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

最简单的选择是to 并且找到所有内容:

extract //body//text()join

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

这里

sel
是一个
Selector
实例。

另一种选择是使用

nltk
clean_html()

>>> import nltk>>> html = """... <div  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 <pre>xpath('//body//text()')</pre> 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()方法。它以单个Unipre字符串的形式返回文档中或标签下的所有文本。

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/378768.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号