使用完整文件,如果您在 xml 中搜索 <div style="float:left;">,您将看到没有导致问题的结束标记。
用坏线:
In [2]: from bs4 import BeautifulSoup
In [3]: collection = BeautifulSoup(open("foo.xml").read(),"lxml")
In [4]: e1 = collection.find('doc').find_all('a')
In [5]: e2 = collection.find_all('doc')[1].find_all('a')
In [6]: len(e1)
6411
In [7]: len(e2)
43
In [8]: len(collection.find_all("a"))
6411
In [9]: len(collection.find('doc').text)
819562
In [10]:len(collection.find_all('doc')[1].text)
3908
In [11]: len(collection.text)
819562
已删除坏行:
In [28]: from bs4 import BeautifulSoup
In [29]: collection = BeautifulSoup(open("foo.xml").read(),"lxml")
In [30]: e1 = collection.find('doc').find_all('a')
In [31]: e2 = collection.find_all('doc')[1].find_all('a')
In [32]: len(e1)
Out[32]: 260
In [33]: len(e2)
Out[33]: 43
In [34]: len(collection.find_all("a"))
Out[34]: 6411
In [35]: len(collection.find('doc').text
Out[35]: 22882
In [36]: len(collection.find_all('doc')[1].text)
Out[36]: 3908
In [37]: len(collection.text)
Out[37]: 819564
对于损坏的 html,您可以将 html.parser 与 bs4 一起使用,这更加宽容:
In [57]: from bs4 import BeautifulSoup
In [58]: collection = BeautifulSoup(open("foo.xml").read(),"html.parser")
In [59]: e1 = collection.find('doc').find_all('a')
In [60]: e2 = collection.find_all('doc')[1].find_all('a')
In [61]: (len(e1))
Out[61]: 260
In [62]: (len(e2))
Out[62]: 43
In [63]: (len(collection.find_all("a")))
Out[63]: 6411
In [64]: (len(collection.find('doc').text))
Out[64]: 22881
In [65]: (len(collection.find_all('doc')[1].text))
Out[65]: 3910
In [66]: (len(collection.text))
Out[66]: 819582
或者使用lxml.html.soupparser结合lxml和bs4:
In [69]: from lxml.html.soupparser import parse
In [70]: xml = parse(open("foo.xml"))
In [71]: e3 = xml.xpath("//doc[1]//a")
In [72]: e4 = xml.xpath("//doc[2]//a")
In [73]: (len(e3))
Out[73]: 260
In [74]: (len(e4))
Out[74]: 43
In [75]: (len(xml.xpath("//a")))
Out[75]: 6411