我想定位文本为“xyz”的“td”,以便在行中查找其他属性。我只有'xyz'和我想得到其他元素在那一行。你知道吗
.
.
.
<tr>
<td>
<a>xyz</a>
</td>
<td>address</td>
<td>phone number</td>
</tr>
.
.
.
我可以通过使用
required = soup.find('a', text = 'xyz')
print(required[0].text)
但我无法定位'td',以便使用find_next_siblings()
获取其他列。你知道吗
预期产量:
xyz
address
phone number
将代码替换为:
订单号:
其中
parent
是Get immediate parent标记,find_next_siblings
返回下一个同级标记的列表。你知道吗在bs4.7.1中,组合
:has
和:contains
的伪类来检索行和tds。你知道吗如果文本中有右
a
标记,则该位以该标记为目标然后检索具有此
a
标记的父行(tr
)最后使用descendant combinator和
td
type selector获得该行中的所有td
。使用列表理解返回列表。你知道吗如果您有modern BeautifulSoup,可以使用CSS选择器
:contains
。然后用find_parent()
方法向后遍历。你知道吗印刷品:
相关问题 更多 >
编程相关推荐