BeautifulSoup中的findAll()跳过多个ID

2024-05-20 21:00:21 发布

您现在位置:Python中文网/ 问答频道 /正文

图像标记中有一个包含多个ID的字符串:

<img id="webfast-uhyubv" alt="" data-type="image" id="comp-jefxldtzbalatamediacontentimage" src="http://webfast.co/images/webfast-logo.png" /> 

soup = bs4.BeautifulSoup(webpage,"html.parser")
images = soup.findAll('img')
for image in images:
    print image

上面的代码只返回id=comp-jefxldtzbalatamediacontentimage

更换

soup = bs4.BeautifulSoup(webpage,"html.parser")

soup = bs4.BeautifulSoup(webpage,"lxml")

返回webfast uhyubv的第一个id

但是,我想按输入行的顺序获取这两个id。你知道吗


Tags: 图像imageidparserimghtmlimageswebpage
1条回答
网友
1楼 · 发布于 2024-05-20 21:00:21

BeautifulSoup存储attributes of a tag in a dictionary。因为字典不能有重复的键,一个id属性会覆盖另一个。您可以使用tag.attrs检查属性字典。你知道吗

>>> soup = BeautifulSoup(tag, 'html.parser')
>>> soup.img.attrs
{'id': 'comp-jefxldtzbalatamediacontentimage', 'alt': '', 'data-type': 'image', 'src': 'http://webfast.co/images/webfast-logo.png'}

>>> soup = BeautifulSoup(tag, 'lxml')
>>> soup.img.attrs
{'id': 'webfast-uhyubv', 'alt': '', 'data-type': 'image', 'src': 'http://webfast.co/images/webfast-logo.png'}

如您所见,我们使用不同的解析器获得id的不同值。这发生在different parsers work differently。你知道吗

使用BeautifulSoup无法同时获取id值。你可以用正则表达式得到它们。但是,use it carefully and as a last resort!

>>> import re
>>> tag = '<img id="webfast-uhyubv" alt="" data-type="image" id="comp-jefxldtzbalatamediacontentimage" src="http://webfast.co/images/webfast-logo.png" />'
>>> ids = re.findall('id="(.*?)"', tag)
>>> ids
['webfast-uhyubv', 'comp-jefxldtzbalatamediacontentimage']

相关问题 更多 >