图像标记中有一个包含多个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。你知道吗
BeautifulSoup存储attributes of a tag in a dictionary。因为字典不能有重复的键,一个
id
属性会覆盖另一个。您可以使用tag.attrs
检查属性字典。你知道吗如您所见,我们使用不同的解析器获得
id
的不同值。这发生在different parsers work differently。你知道吗使用BeautifulSoup无法同时获取
id
值。你可以用正则表达式得到它们。但是,use it carefully and as a last resort!相关问题 更多 >
编程相关推荐