BeautifulSoup 4、findNext() 函数 [英] BeautifulSoup 4, findNext() function
问题描述
我正在使用 BeautifulSoup 4,我有这个 html 代码:
I'm playing with BeautifulSoup 4 and I have this html code:
</tr>
<tr>
<td id="freistoesse">Giraffe</td>
<td>14</td>
<td>7</td>
</tr>
我想匹配 <td>
标签之间的两个值,所以这里是 14 和 7.
I want to match both values between <td>
tags so here 14 and 7.
我试过了:
giraffe = soup.find(text='Giraffe').findNext('td').text
但这仅匹配 14
.如何将这两个值与此函数匹配?
but this only matches 14
. How can I match both values with this function?
推荐答案
使用find_all
代替findNext
:
import bs4 as bs
content = '''
<tr>
<td id="freistoesse">Giraffe</td>
<td>14</td>
<td>7</td>
</tr>'''
soup = bs.BeautifulSoup(content)
for td in soup.find('td', text='Giraffe').parent.find_all('td'):
print(td.text)
收益
Giraffe
14
7
<小时>
或者,您可以使用find_next_siblings
(也称为fetchNextSiblings
):
for td in soup.find(text='Giraffe').parent.find_next_siblings():
print(td.text)
收益
14
7
<小时>
说明:
请注意,soup.find(text='Giraffe')
返回一个 NavigableString.
Note that soup.find(text='Giraffe')
returns a NavigableString.
In [30]: soup.find(text='Giraffe')
Out[30]: u'Giraffe'
要获取关联的 td
标记,请使用
To get the associated td
tag, use
In [31]: soup.find('td', text='Giraffe')
Out[31]: <td id="freistoesse">Giraffe</td>
或
In [32]: soup.find(text='Giraffe').parent
Out[32]: <td id="freistoesse">Giraffe</td>
一旦你有了 td
标签,你就可以使用 find_next_siblings
:
Once you have the td
tag, you could use find_next_siblings
:
In [35]: soup.find(text='Giraffe').parent.find_next_siblings()
Out[35]: [<td>14</td>, <td>7</td>]
<小时>
附注.BeautifulSoup 添加了使用下划线代替 CamelCase 的方法名称.他们做同样的事情,但符合 PEP8 风格指南的建议.因此,更喜欢 find_next_siblings
而不是 fetchNextSiblings
.
这篇关于BeautifulSoup 4、findNext() 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!