[英]How do I get an item but only if is a sibiling of a certain tag
I have a long html but here's a fragment:我有一个很长的 html 但这是一个片段:
<tr>
<td data-bind="text:name, css: isActive() ? 'variable-active': 'variable-inactive'" class="variable-active">Vehicle</td>
<td data-bind="text:value">Ford</td>
</tr>
<tr>
<td data-bind="text:name, css: isActive() ? 'variable-active': 'variable-inactive'" class="variable-inactive">Model</td>
<td data-bind="text:value">Focus</td>
</tr>
I want to get all the content tags based on if it is "variable-active", and then get the value from the next 'td' tag.我想根据它是否为“变量活动”来获取所有内容标签,然后从下一个“td”标签中获取值。 In this case, as the second class tag is "variable-inactive", the output should be:
在这种情况下,由于第二个 class 标记是“变量无效”,因此 output 应该是:
"Vehicle - Ford"
I managed to get the first tags based on the "variable-active" but I can't get the second values from the other tags.我设法根据“变量活动”获取第一个标签,但我无法从其他标签中获取第二个值。 This is my code:
这是我的代码:
from bs4 import BeautifulSoup
with open ("html.html","r") as f:
doc = BeautifulSoup(f,"html.parser")
tag = doc.findAll("tr")[0]
print(tag.findAll(class_="variable-active")[0].contents[0]) #vehicle
tag.findNextSibling(class_="variable-active") # nothing
You want to structure your search a little bit different:您想构建您的搜索有点不同:
tag = soup.findAll("tr")[0]
tag1 = tag.find(class_="variable-active") # <-- use .find
tag2 = tag1.findNextSibling() # <-- use tag1.findNextSibling() to find next sibling tag
print(tag1.text) # <-- use .text to get all text from tag
print(tag2.text)
Prints:印刷:
Vehicle
Ford
Another version using CSS selectors:另一个使用 CSS 选择器的版本:
data = soup.select(".variable-active, .variable-active + *")
print(" - ".join(d.text for d in data))
Prints:印刷:
Vehicle - Ford
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.