[英]Beautiful Soup 4 CSS sibling selector
我正在嘗試使用Beatiful Soup 4 abd Python 2.7解析從InDesign文檔導出的一些HTML。 我試圖通過使用CSS兄弟選擇器找到一個特定的標簽。 我可以通過CSS選擇器選擇它的兄弟,然后使用Beautiful Soup find_next_sibling()
方法來訪問我想要的標簽,但我無法通過CSS選擇器直接選擇它。
我已經驗證了當我在純CSS / JS( http://jsfiddle.net/Sj63x/1/ )中嘗試時,選擇器本身是有效的。 我也試過使用Beautiful Soup主頁上推薦的所有三種解析器。
相關代碼發布在下面(文本在JS小提琴中):
text = BeautifulSoup(text)
'''this finds the sibling'''
sibling = text.select(".Book-Title-")
print(sibling[0].string)
'''this finds the sibling I am looking for'''
targetText = sibling[0].find_next_sibling()
print(targetText.string)
'''This should find the same text but returns an empty list'''
targetText2 = text.select(".Book-Title- ~.Text")
print(targetText2)
'''Other attempted variations - also return empty lists'''
targetText3 = text.select(".Book-Title- ~ .Text")
targetText4 = text.select(".Book-Title- + .Text")
請嘗試使用此選擇器:
targetText2 = text.select(".Book-Title- + .Text")
或者在波形符和兄弟之間添加一個空格:
targetText2 = text.select(".Book-Title- ~ .Text")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.