簡體   English   中英

Beautifulsoup無法通過文字找到標簽

[英]Beautifulsoup can't find tag by text

Beautifulsoup突然找不到它的文字標簽。

我有一個html,其中出現此標記:

<span class="date">Telefon: <b>+421 902 808 344</b></span>

BS4找不到此標簽:

telephone = soup.find('span',{'text':re.compile('.*Telefon.*')})
print telephone

>>> None

我嘗試了很多方法

find('span',text='Telefon: ')find('span', text=re.compile('Telefon: .*')

但沒有任何作用。 我已經嘗試將html.parser更改為lxml

可能有什么不對?

BeautifulSoup認為字符串Telefon:作為span標記內的bs4.element.NavigableString 所以你可以找到它

import bs4
import re

soup = bs4.BeautifulSoup('<span class="date">Telefon: <b>+421 902 808 344</b></span>')
for span in soup.find_all('span', {'class':"date"}):
    if span.find(text=re.compile('Telefon:')):
        for text in span.stripped_strings:
            print(text)
# Telefon:
# +421 902 808 344

或者,您可以直接使用lxml:

import lxml.html as LH

root = LH.fromstring('<span class="date">Telefon: <b>+421 902 808 344</b></span>')

for span in root.xpath('//span[@class="date" and contains(text(), "Telefon:")]'):
    print(span.text_content())
    # Telefon: +421 902 808 344

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM