我有一個文件充滿句子包裝在格式良好的XML(xmllint和tidylib這樣說)。 所以這個xml看起來像這樣:xml解析終止莫名其妙
<a id="100" attr1="text" attr1="text" attr1="text">
<tagname id="1">
This is my sentence.
</tagname>
</a>
<a id="101" attr1="text" attr1="text" attr1="text">
<tagname id="1">
This is my sentence.
</tagname>
</a>
等等。
我用下面的代碼(從ID 1在這種情況下,以85)提取與屬性句子
a1 = open(r"file.xml",'r')
a = a1.readlines()
a1.close()
soup = BeautifulSoup(str(a))
for i in range(1,85):
a = soup.find('a', {'id': i})
achild = a.find('tagname')
tagnametext = achild.contents
print tagnametext
一切打印很好,直到句子84,在該收到錯誤: achild = a.find('tagname') AttributeError:'NoneType'對象沒有屬性'find'
每一組......都是用for循環生成的,所以xml都是一樣的。 我用不同數量的句子嘗試過不同的文件。發生錯誤的ID也會發生變化。 這是美麗的限制嗎? 它不能掃描一定數量的行?
這是什麼ID號84是什麼樣子? – TerryA