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

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

问题描述

我与抓一些的HREF在TD所需的语法挣扎。
该表,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?

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

,然后你在里面搜索,例如用于&LT; A&GT; 标签,其母公司为一个&LT; TD&GT;

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天全站免登陆