Подтвердить что ты не робот

Использование BeautifulSoup для поиска тега HTML, который содержит определенный текст

Я пытаюсь получить элементы в HTML-документе, которые содержат следующий шаблон текста: #\S {11}

<h2> this is cool #12345678901 </h2>

Итак, предыдущее совпадало бы с помощью:

soup('h2',text=re.compile(r' #\S{11}'))

И результаты будут примерно такими:

[u'blahblah #223409823523', u'thisisinteresting #293845023984']

Я могу получить весь текст, который соответствует (см. строку выше). Но я хочу, чтобы родительский элемент текста соответствовал, поэтому я могу использовать это как отправную точку для перемещения по дереву документов. В этом случае я бы хотел, чтобы все элементы h2 возвращались, а не текст.

Идеи?

4b9b3361

Ответ 1

from BeautifulSoup import BeautifulSoup
import re

html_text = """
<h2>this is cool #12345678901</h2>
<h2>this is nothing</h2>
<h1>foo #126666678901</h1>
<h2>this is interesting #126666678901</h2>
<h2>this is blah #124445678901</h2>
"""

soup = BeautifulSoup(html_text)


for elem in soup(text=re.compile(r' #\S{11}')):
    print elem.parent

Печать

<h2>this is cool #12345678901</h2>
<h2>this is interesting #126666678901</h2>
<h2>this is blah #124445678901</h2>

Ответ 2

Операции поиска BeautifulSoup доставляют [список] BeautifulSoup.NavigableString объектов, когда text= используется в качестве критерия в отличие от BeautifulSoup.Tag в других случаях. Проверьте объект __dict__, чтобы увидеть доступные вам атрибуты. Из этих атрибутов parent предпочтительнее previous из-за изменений в BS4.

from BeautifulSoup import BeautifulSoup
from pprint import pprint
import re

html_text = """
<h2>this is cool #12345678901</h2>
<h2>this is nothing</h2>
<h2>this is interesting #126666678901</h2>
<h2>this is blah #124445678901</h2>
"""

soup = BeautifulSoup(html_text)

# Even though the OP was not looking for 'cool', it more understandable to work with item zero.
pattern = re.compile(r'cool')

pprint(soup.find(text=pattern).__dict__)
#>> {'next': u'\n',
#>>  'nextSibling': None,
#>>  'parent': <h2>this is cool #12345678901</h2>,
#>>  'previous': <h2>this is cool #12345678901</h2>,
#>>  'previousSibling': None}

print soup.find('h2')
#>> <h2>this is cool #12345678901</h2>
print soup.find('h2', text=pattern)
#>> this is cool #12345678901
print soup.find('h2', text=pattern).parent
#>> <h2>this is cool #12345678901</h2>
print soup.find('h2', text=pattern) == soup.find('h2')
#>> False
print soup.find('h2', text=pattern) == soup.find('h2').text
#>> True
print soup.find('h2', text=pattern).parent == soup.find('h2')
#>> True