我希望在呈现HTML之后在网站上显示所有文本。我使用Scrapy框架在Python中工作。 With xpath('//body//text()')
我能够得到它,但带有HTML标签,我只想要文本。任何解决方案?谢谢 !如何从Scrapy网站获取所有纯文本?
9
A
回答
19
最简单的办法是找到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()
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.html
的text_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 !
2
你试过了吗?
xpath('//body//text()').re('(\w+)')
OR
xpath('//body//text()').extract()
+1
这实际上工作得很好,但仍然返回一些html标签和其他。 – tomasyany
相关问题
- 1. Scrapy从网页获取文本
- 2. 从网站获取文本
- 3. 如何从网站获取所有网站链接?
- 4. 使用Scrapy获取整个网站中的所有网址
- 5. 用Puppeteer获取所有纯文本
- 6. 如何从网站获取所有有效的网址?
- 7. 如何从kendoEditor获取纯文本?
- 8. 如何使用scrapy来抓取网站中的所有项目
- 9. 从QLineEdit获取纯文本
- 10. 从SunAwtCanvas获取纯文本
- 11. 从网站获取所有用户
- 12. 从RTF文本中获取纯文本
- 13. 获取网站集中的所有网站而没有获取所有列表?
- 14. 如何从C#中的网页获取所有显示文本
- 15. VS2012 - 如何获取现有网站上的所有文件?
- 16. scrapy抓取网站
- 17. 如何从无限滚动网站上刮取所有内容? scrapy
- 18. 如何从整个文本中获取网站网址?
- 19. 如何从网站获取css文件?
- 20. 从网站获取JSON文本
- 21. Python - 从报纸网站获取文本
- 22. 用VBA从网站获取文本
- 23. 使用JSoup从网站获取文本
- 24. 从警报获取文本在网站
- 25. 如何从网站访问者获取所有客户信息?
- 26. 如何从网站获取所有图片?
- 27. 如何从Google缓存中获取网站的所有内容?
- 28. 如何使用Python从本网站刮取所有数据
- 29. 如何从文本字段中获取纯文本?
- 30. 如何从java.mail获取HTML文本/纯文本
我删除了我的问题..我已经使用了下面的代码html = sel.select(“// body // text()”) tree = lxml.html.fromstring(html) item ['description'] = tree.text_content()。strip()但是我得到了\t is_full_html = _looks_like_full_html_unicode(html) \t exceptions.TypeError:期望的字符串或缓冲区..erro。出错了 – Backtrack
'nltk'对我来说效果最好 – user4421975
就像更新一样,'nltk'弃用了他们的'clean_html'方法,而是建议: 'NotImplementedError:要删除HTML标记,请使用BeautifulSoup的get_text()函数 ' – TheNastyOne