使用Beautifutsoup从HTML标签中检索所有名称

发布于 2025-01-28 17:11:57 字数 710 浏览 1 评论 0原文

我设法用美丽的汤来设置,并找到所需的标签。如何提取标签中的所有名称?

tags = soup.find_all("a")
print(tags)

运行上述代码后,我得到了以下输出,

[<a href="/wiki/Alfred_the_Great" title="Alfred the Great">Alfred the Great</a>, <a class="mw-redirect" href="/wiki/Elizabeth_I_of_England" title="Elizabeth I of England">Queen Elizabeth I</a>, <a href="/wiki/Family_tree_of_Scottish_monarchs" title="Family tree of Scottish monarchs">Family tree of Scottish monarchs</a>, <a href="/wiki/Kenneth_MacAlpin" title="Kenneth MacAlpin">Kenneth MacAlpin</a>]

该如何检索名称,Alfred the Great the Great,伊丽莎白一世,肯尼斯·麦卡尔平(Kenneth Macalpin)等?我需要使用正则表达式吗?使用.String给了我一个错误

I managed to setup by Beautiful Soup and find the tags that I needed. How do I extract all the names in the tags?

tags = soup.find_all("a")
print(tags)

After running the above code, I got the following output

[<a href="/wiki/Alfred_the_Great" title="Alfred the Great">Alfred the Great</a>, <a class="mw-redirect" href="/wiki/Elizabeth_I_of_England" title="Elizabeth I of England">Queen Elizabeth I</a>, <a href="/wiki/Family_tree_of_Scottish_monarchs" title="Family tree of Scottish monarchs">Family tree of Scottish monarchs</a>, <a href="/wiki/Kenneth_MacAlpin" title="Kenneth MacAlpin">Kenneth MacAlpin</a>]

How do I retrieve the names, Alfred the Great,Queen Elizabeth I, Kenneth MacAlpin, etc? Do i need to use regular expression? Using .string gave me an error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沦落红尘 2025-02-04 17:11:57

您可以在标签上迭代并使用tag.get('title')获取标题值。

其他一些做同样的方法:
https://www.crummy.com/ /#属性

You can iterate over the tags and use tag.get('title') to get the title value.

Some other ways to do the same:
https://www.crummy.com/software/BeautifulSoup/bs4/doc/#attributes

原野 2025-02-04 17:11:57

无需应用re。 轻松获取所有名称:

html='''
<html>
 <body>
  <a href="/wiki/Alfred_the_Great" title="Alfred the Great">
   Alfred the Great
  </a>
  ,
  <a class="mw-redirect" href="/wiki/Elizabeth_I_of_England" title="Elizabeth I of England">
   Queen Elizabeth I
  </a>
  ,
  <a href="/wiki/Family_tree_of_Scottish_monarchs" title="Family tree of Scottish monarchs">
   Family tree of Scottish monarchs
  </a>
  ,
  <a href="/wiki/Kenneth_MacAlpin" title="Kenneth MacAlpin">
   Kenneth MacAlpin
  </a>
 </body>
</html>

'''

from bs4 import BeautifulSoup

soup = BeautifulSoup(html,'lxml')

#print(soup.prettify())

for name in soup.find_all('a'):
    txt = name.get('title')
    #OR
    #txt = name.get_text(strip=True)
    print(txt)

您可以通过迭代所有标签,然后调用标题属性或get_text()或.find(text = true) 输出来

Alfred the Great
Queen Elizabeth I
Family tree of Scottish monarchs
Kenneth MacAlpin

No need to apply re. You can easily grab all the names by iterating all a tags then call title attribute or get_text() or .find(text=True)

html='''
<html>
 <body>
  <a href="/wiki/Alfred_the_Great" title="Alfred the Great">
   Alfred the Great
  </a>
  ,
  <a class="mw-redirect" href="/wiki/Elizabeth_I_of_England" title="Elizabeth I of England">
   Queen Elizabeth I
  </a>
  ,
  <a href="/wiki/Family_tree_of_Scottish_monarchs" title="Family tree of Scottish monarchs">
   Family tree of Scottish monarchs
  </a>
  ,
  <a href="/wiki/Kenneth_MacAlpin" title="Kenneth MacAlpin">
   Kenneth MacAlpin
  </a>
 </body>
</html>

'''

from bs4 import BeautifulSoup

soup = BeautifulSoup(html,'lxml')

#print(soup.prettify())

for name in soup.find_all('a'):
    txt = name.get('title')
    #OR
    #txt = name.get_text(strip=True)
    print(txt)

Output:

Alfred the Great
Queen Elizabeth I
Family tree of Scottish monarchs
Kenneth MacAlpin
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文