python – 使用BeautifulSoup,我可以快速遍历特定的父元素吗?
发布时间:2020-12-20 11:14:45 所属栏目:Python 来源:网络整理
导读:假设我引用 HTML页面中的表格内的元素,如下所示: someEl = soup.findAll(text = "some text") 我知道这个元素是嵌入在一个表中的,有没有办法找到父表而不必多次调用.parent? table.......tr....tdcenterfont..bsome text/b/font/center/td....trtable 解决
假设我引用
HTML页面中的表格内的元素,如下所示:
someEl = soup.findAll(text = "some text") 我知道这个元素是嵌入在一个表中的,有没有办法找到父表而不必多次调用.parent? <table...> .. .. <tr>....<td><center><font..><b>some text</b></font></center></td>....<tr> <table> 解决方法
看看findParents,它有一个类似于findAll的形式:
soup = BeautifulSoup("<table>...</table>") for text in soup.findAll(text='some text') table = text.findParents('table')[0] # table is your now your most recent `<table>` parent 这里有the docs for findAllPrevious,也找到了父母. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |