2017-06-11 51 views
0

我試圖從dictionary.com的span標記的「data-syllable」部分獲取音節列表(如下所示)。當我用我的代碼嘗試這樣做時,它給了我一個沒有填充([None,None,None,None,None,None,None,None,None,None])的列表,我不知道如何解決它。請幫忙。如何從Python中的一段html代碼中獲取項目列表?

這裏就是我想從

[span class="dbox-bold">prob</span>, <span class="dbox-bold" data- 
syllable="im·prob·a·bly, ">improbably, </span>, <span 
class="dbox-bold" data-syllable="im·prob·a·ble·ness, ">improbableness, 
</span>, <span class="dbox-bold" data-syllable="su·per·im·prob·a·ble, 
">superimprobable, </span>, <span class="dbox-bold" data- 
syllable="su·per·im·prob·a·ble·ness, ">superimprobableness, </span>, 
<span class="dbox-bold" data-syllable="su·per·im·prob·a·bly, 
">superimprobably, </span>, <span class="dbox-bold">improbable</span>, 
<span class="dbox-bold" data-syllable="imˌprobaˈbility, 
">improbability, </span>, <span class="dbox-bold" data-syllable="im 
ˈprobableness, ">improbableness, </span>, <span class="dbox-bold" data- 
syllable="imˈprobably, ">improbably, </span>] 

獲取數據音節標籤========================== ==============================================

這裏是我的代碼:

a = [item for item in soup.find_all('span','dbox-bold')] 
    find = [item.find(name='data-syllable') for item in a] 
    return find 

print(count_syllables('improbable')) 

回答

0

您正在尋找name屬性。要查找data-syllable屬性,您需要查看每個項目的密鑰attrs

find = [item.attrs.get('data-syllable', None) for item in a]

現在你可以看到在find值。

+0

謝謝你的幫助!但是,當我嘗試返回查找,它仍然給我的名單NONE – emptybladder

+0

它現在的作品!非常感謝! – emptybladder

+0

當我返回查找時,我遇到了一個問題,它仍然給我無列表中的一些值。你有什麼想法,爲什麼這可能是?例如,我跑了隱形詞,它給了我[無,無,'in'vis·i·bil·i·ty,','in·vis·i·ble·ness,','in·vis·我不知道','不可見','不可見','不可見','不可見','不可見','不可見','不可見' ','invisibly,'],而不是完整列表 – emptybladder

相關問題