美丽的汤越来越tag.id [英] beautiful soup getting tag.id

查看:75
本文介绍了美丽的汤越来越tag.id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从页面获取div ID列表.当我打印出属性时,我会列出ID.

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

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

导致:

{'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']}

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

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?

推荐答案

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

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')

tag.id不起作用的原因是它等同于tag.find('id'),由于未找到id标记,因此导致None(

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

这篇关于美丽的汤越来越tag.id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆