使用BeautifulSoup获取标签名称 [英] Getting Tag Names with BeautifulSoup

查看:228
本文介绍了使用BeautifulSoup获取标签名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

from bs4 import BeautifulSoup
source_code = """<a href="#" name="linkName">ok</a>"""
soup = BeautifulSoup(source_code)
print soup.a.? #find the object name

使用上面显示的代码,我试图打印锚标记'name',它是linkName,但是我不确定我将使用哪个模块或对象,我已经尝试过contentsnametag_name_re.

Using the code displayed above, i am trying to print the anchor tags 'name' which is linkName but i'm not sure which module or object i will be using, i have tried contents,name and tag_name_re.

有人可以帮我吗?谢谢!

Can anybody help me out? thanks!

推荐答案

您已经回答了问题.

soup.a['name']

修改

如果您有多个a元素,则可以执行以下操作:

If you have more than one a element, you can do this:

x = """<x><a name="foo"/><a name="bar"/></x>"""
s = bs4.BeautifulSoup(x)
for a in s.findChildren("a"):
    print(a["name"])

这篇关于使用BeautifulSoup获取标签名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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