【发布时间】:2015-10-02 00:43:32
【问题描述】:
我在 HTML 中有一个很大的长表,因此标签不会相互嵌套。它看起来像这样:
<tr>
<td>A</td>
</tr>
<tr>
<td class="x">...</td>
<td class="x">...</td>
<td class="x">...</td>
<td class="x">...</td>
</tr>
<tr>
<td class ="y">...</td>
<td class ="y">...</td>
<td class ="y">...</td>
<td class ="y">...</td>
</tr>
<tr>
<td>B</td>
</tr>
<tr>
<td class="x">...</td>
<td class="x">...</td>
<td class="x">...</td>
<td class="x">...</td>
</tr>
<tr>
<td class ="y">I want this</td>
<td class ="y">and this</td>
<td class ="y">and this</td>
<td class ="y">and this</td>
</tr>
所以首先我想搜索树以找到“B”。然后我想在 B 之后但在表格的下一行以“C”重新开始之前获取每个 td 标记的文本,其中 y 类。
我试过这个:
results = soup.find_all('td')
for result in results:
if result.string == "B":
print(result.string)
这得到了我想要的字符串 B。但现在我试图在这之后找到所有东西,但我没有得到我想要的。
for results in soup.find_all('td'):
if results.string == 'B':
a = results.find_next('td',class_='y')
这给了我在“B”之后的下一个 td,这是我想要的,但我似乎只能得到第一个 td 标记。我想在“B”之后但在“C”之前获取所有具有 y 类的标签(C 未显示在 html 中,但遵循相同的模式),并且我想将其添加到列表中。
我的结果列表是:
[['I want this'],['and this'],['and this'],['and this']]
【问题讨论】:
标签: python html beautifulsoup html-parsing