How can I get all the plain text from a website with Scrapy?

前端 未结 3 1446
夕颜
夕颜 2020-11-30 03:24

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()\')

3条回答
  •  难免孤独
    2020-11-30 04:16

    The easiest option would be to extract //body//text() and join everything found:

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

    where sel is a Selector instance.

    Another option is to use nltk's clean_html():

    >>> import nltk
    >>> 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. ... 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(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 !"

    Another option is to use BeautifulSoup's 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 !
    

    Another option is to use lxml.html's 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 !
    

提交回复
热议问题