gpt4 book ai didi

python - 使用 beautifulsoup 4 进行 xml 会导致奇怪的行为(内存问题?)

转载 作者:行者123 更新时间:2023-11-30 23:44:58 25 4
gpt4 key购买 nike

我的行为很奇怪

>>> from bs4 import BeautifulSoup

>>> smallfile = 'small.xml' #approx 600bytes
>>> largerfile = 'larger.xml' #approx 2300 bytes
>>> len(BeautifulSoup(open(smallfile, 'r'), ['lxml', 'xml']))
1
>>> len(BeautifulSoup(open(largerfile, 'r'), ['lxml', 'xml']))
0

small.xml 的内容:

<?xml version="1.0" encoding="us-ascii"?>
<Catalog>
<CMoverMissile id="HunterSeekerMissile">
<MotionPhases index="1">
<Driver value="Guidance"/>
<Acceleration value="3200"/>
<MaxSpeed value="2.9531"/>
<Clearance value="0.5"/>
<ClearanceLookahead value="3"/>
<Outro value="-4.5,-4.25"/>
<YawPitchRoll value="MAX"/>
</MotionPhases>
<MotionPhases index="2">
<Driver value="Guidance"/>
<Acceleration value="4"/>
<MaxSpeed value="2.9531"/>
<Clearance value="0.5"/>
<ClearanceLookahead value="3"/>
<Outro value="-2.25,-2"/>
<YawPitchRoll value="MAX"/>
</MotionPhases>
</CMoverMissile>
</Catalog>

largerfile 只是较小的文件,但用空格和换行符填充(在最后两个标记之间,以防相关)。 IE 中,两种情况下 xml 的结构和内容应该相同。

在极少数情况下,处理较大的文件实际上会产生部分结果,其中仅解析了一小部分 xml。我似乎无法可靠地重现当时的情况。

由于BeautifulSoup使用lxml,我测试了一下lxml是否可以独立处理文件。 lxml 似乎能够解析这两个文件。

>>> from lxml import etree
>>> tree = etree.parse(smallfile)
>>> len(etree.tostring(tree))
547
>>> tree = etree.parse(largerfile)
>>> len(etree.tostring(tree))
2294

我正在使用

  • 配备 1GB 内存的上网本
  • Windows 7
  • lxml 2.3(安装时遇到一些问题,我希望不可靠的安装不会导致问题)
  • 美汤4.0.1
  • python 3.2(我还安装了 python 2.7x,但此代码一直使用 3.2)

什么可能会阻止较大的文件得到正确处理?我目前的怀疑是一些奇怪的内存问题,因为文件大小似乎有所不同,也许与 BeautifulSoup 4 与 lxml 交互方式的一些错误有关。

编辑:为了更好地说明...

>>> smallsoup = BeautifulSoup(smallfile), ['lxml', 'xml'])
>>> smallsoup
<?xml version="1.0" encoding="utf-8"?>
<Catalog>
<CMoverMissile id="HunterSeekerMissile">
<MotionPhases index="1">
<Driver value="Guidance"/>
<Acceleration value="3200"/>
<MaxSpeed value="2.9531"/>
<Clearance value="0.5"/>
<ClearanceLookahead value="3"/>
<Outro value="-4.5,-4.25"/>
<YawPitchRoll value="MAX"/>
</MotionPhases>
<MotionPhases index="2">
<Driver value="Guidance"/>
<Acceleration value="4"/>
<MaxSpeed value="2.9531"/>
<Clearance value="0.5"/>
<ClearanceLookahead value="3"/>
<Outro value="-2.25,-2"/>
<YawPitchRoll value="MAX"/>
</MotionPhases>
</CMoverMissile>
</Catalog>
>>> largersoup = BeautifulSoup(largerfile, ['lxml', 'xml'])
>>> largersoup
<?xml version="1.0" encoding="utf-8"?>

>>>

>>> repr(open(largefile, 'r').read())
'\'<?xml version="1.0" encoding="us-ascii"?>\\n<Catalog>\\n<CMoverMissile id="HunterSeekerMissile">\\n<MotionPhases index="1">\\n<Driver value="Guidance"/>\\n<Acceleration value="3200"/>\\n<MaxSpeed value="2.9531"/>\\n<Clearance value="0.5"/>\\n<ClearanceLookahead value="3"/>\\n<Outro value="-4.5,-4.25"/>\\n<YawPitchRoll value="MAX"/>\\n</MotionPhases>\\n<MotionPhases index="2">\\n<Driver value="Guidance"/>\\n<Acceleration value="4"/>\\n<MaxSpeed value="2.9531"/>\\n<Clearance value="0.5"/>\\n<ClearanceLookahead value="3"/>\\n<Outro value="-2.25,-2"/>\\n<YawPitchRoll value="MAX"/>\\n</MotionPhases>\\n</CMoverMissile> </Catalog>\''

注意: 和\'' 之间有很多空格(可能不会在浏览器中显示)

最佳答案

len(soup)返回 len(soup.contents) 即直系子代的数量(在本例中为单个子代 <Catalog> )。

BeautifulSoup无法解析largerfile所以len(soup) == 0

关于python - 使用 beautifulsoup 4 进行 xml 会导致奇怪的行为(内存问题?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9837713/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com