我試圖讀取和列表的文本在TD屬性錯誤與讀取HTML代碼,並基於數據屬性與beautifulsoup beautifulsoup
tr=BeautifulSoup(str(input),'lxml')
tags=tr.findAll('td')
for t in tags:
if t.attrs['data-property']== 'OSVersion':
ver=t.text
這給了我的錯誤,沒有細節
KeyError: 'data-property'
請看下面的例子tr提取爲輸入
<tr >
<td class=" resizable reorderable" data-property="OSVersion">10.2.1</td>
<td class=" resizable reorderable" data-property="DisplayModel">iPad Mini 4 (64 GB Space Gray)</td>
<td class=" resizable reorderable" data-property="PhoneNumber"></td>
<td class="grid_customvariable_colsize resizable reorderable" data-property="DeviceCustomAttributeDetails"></td>
<td class=" resizable reorderable" data-property="DeviceTagDetails"></td>
<td class=" resizable reorderable" data-property="EnrollmentStatusName"> <div class="grid_resizable_col">Enrolled</div>
</td>
<td class=" resizable reorderable" data-property="ComplianceStatusName"> <div class="grid_resizable_col">Compliant</div>
</td>
<td class=" resizable reorderable" data-property="IMEI"></td>
<td class=" resizable reorderable" data-property="LocationGroupName">iOS</td>
<td class=" resizable reorderable" data-property="IsCompromisedYN">No</td>
<td class=" resizable reorderable" data-property="HomeCarrier">Not Reported </td>
<td class=" resizable reorderable" data-property="CurrentCarrier">Not Reported </td>
<td class=" resizable reorderable" data-property="WiFiIPAddress"></td>
<td class=" resizable reorderable" data-property="Notes"></td>
<td class=" resizable reorderable" data-property="WnsStatus"> <span>Disconnected</span>
</td>
<td class=" resizable reorderable" data-property="DmLastSeenTime"> <span class="icon arrow_down_stretched red">-</span>
</td>
</tr>
如果我把單個字典作爲f然而,它工作正常
d={'class': ['', 'resizable', 'reorderable'], 'data-property': 'FriendlyName'}
print d['data-property']
任何人有想法如何解決它?
謝謝
在'BeautifulSoup'重命名變量'str' - 這是一個保留字。 –
試過,那不是它,如果多數民衆贊成的原因,它不會運行傳遞到下一行 – ikel