text
參數(現在命名爲string
)實際上檢查以匹配期望的標準的元件的.string
- 在這種情況下,正則表達式^Location.*
。現在
,有什麼特別的.string
屬性 - 它的價值將是None
如果一個標籤有多個孩子:
如果一個標籤包含一個以上的事情,那麼目前還不清楚是什麼 .string應參照,所以.string被定義爲無
而且,你li
元素實際上已經多個孩子 - 一個文本節點Location:
和a
元素。因此,沒有結果。
相反,找到的文本元素,然後獲得所需的元素:
In [1]: import re
In [2]: from bs4 import BeautifulSoup
In [3]: html_text = '<li>Location:<a href="tweetLocation">tweetLocation</a></li>'
In [4]: soup = BeautifulSoup(html_text, "html.parser")
In [5]: soup.find(text=re.compile(r'^Location.*')).find_parent('li')
Out[5]: <li>Location:<a href="tweetLocation">tweetLocation</a></li>
In [6]: soup.find(text=re.compile(r'^Location.*')).next_sibling.get_text()
Out[6]: 'tweetLocation'
的可能的複製[BeautifulSoup - 通過文本標籤內搜索(https://stackoverflow.com/questions/31958637/ beautifulsoup搜索按文本內,一個標籤) –