如何在美丽的汤中获得嵌套元素 [英] How to get a nested element in beautiful soup

查看:20
本文介绍了如何在美丽的汤中获得嵌套元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为在 td 中获取一些 href 所需的语法而苦苦挣扎.table、tr 和 td 元素没有任何类或 id.

I am struggling with the syntax required to grab some hrefs in a td. The table, tr and td elements dont have any class's or id's.

如果我想抓住这个例子中的锚点,我需要什么?

If I wanted to grab the anchor in this example, what would I need?

<td > <一个 >...

< tr > < td > < a >...

谢谢

推荐答案

根据文档,您首先创建一个解析树:

As per the docs, you first make a parse tree:

import BeautifulSoup
html = "<html><body><tr><td><a href='foo'/></td></tr></body></html>"
soup = BeautifulSoup.BeautifulSoup(html)

然后你在其中搜索,例如 <a> 标签,其直接父是 <td>:

and then you search in it, for example for <a> tags whose immediate parent is a <td>:

for ana in soup.findAll('a'):
  if ana.parent.name == 'td':
    print ana["href"]

这篇关于如何在美丽的汤中获得嵌套元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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