Python 美丽的汤得到 tag.id

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24962673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:31:24  来源:igfitidea点击:

beautiful soup getting tag.id

pythonhtmlbeautifulsouphtml-parsing

提问by klreeher

I'm attempting to get a list of div ids from a page. When I print out the attributes, I get the ids listed.

我正在尝试从页面中获取 div id 列表。当我打印出属性时,我会得到列出的 id。

for tag in soup.find_all(class_="bookmark blurb group") :
  print(tag.attrs)

results in:

结果是:

{'id': 'bookmark_8199633', 'role': 'article', 'class': ['bookmark', 'blurb', 'group']}
{'id': 'bookmark_7744613', 'role': 'article', 'class': ['bookmark', 'blurb', 'group']}
{'id': 'bookmark_7338591', 'role': 'article', 'class': ['bookmark', 'blurb', 'group']}
{'id': 'bookmark_7338535', 'role': 'article', 'class': ['bookmark', 'blurb', 'group']}
{'id': 'bookmark_4530078', 'role': 'article', 'class': ['bookmark', 'blurb', 'group']}

So I know there ARE ids. However, when I print out tag.id instead, I just get a list of "None". What am I doing wrong here?

所以我知道有ID。但是,当我打印出 tag.id 时,我只会得到一个“无”列表。我在这里做错了什么?

采纳答案by alecxe

You can access tag's attributes by treating the tag like a dictionary (documentation):

您可以通过将标签视为字典(文档)来访问标签的属性:

for tag in soup.find_all(class_="bookmark blurb group") :
    print tag.get('id')

The reason tag.iddidn't work is that it is equivalent to tag.find('id'), which results into Nonesince there is no idtag found (documentation).

tag.id不起作用的原因是它等效于tag.find('id')None由于没有id找到标签(文档),因此结果为。