how to get text from within a tag, but ignore other child tags

后端 未结 1 1826
北荒
北荒 2020-12-06 02:21

I am working with beautiful soup. I have a html string:

ignore thisget this

How do I retrieve \"g

1条回答
  •  Happy的楠姐
    2020-12-06 02:59

    You can get the div text just not recursively retrieving the children texts:

    >>> from bs4 import BeautifulSoup
    >>> soup = BeautifulSoup('
    ignore thisget this
    ') >>> soup.div.find(text=True, recursive=False) u'get this'

    This works independently of the position of the text with respect of the children:

    >>> soup = BeautifulSoup('
    get thisignore this
    ') >>> soup.div.find(text=True, recursive=False) u'get this'

    0 讨论(0)
提交回复
热议问题