下面提到的html是表中的一个“tr”块。 我的目标是从“tr”块中提取每个值。你知道吗
HTML:
<tr><th colspan="7" class="tablehead">
Historical Data for NIFTY 50
</th></tr>
<tr><th colspan="7" class="tablehead">
For the period 01-01-2019 to 10-01-2019
</th></tr>
<tr>
<th >Date</th>
<th >Open</th>
<th >High</th>
<th >Low</th>
<th >Close</th>
<th>Shares Traded</th>
<th >Turnover <br/>(<!--Rs.--> <img src="/images/rup_t1.gif"
alt = "Rs." border="0"> Cr)</th>
</tr>
<tr>
<td class="date"><nobr>01-Jan-2019</nobr></td>
<td class="number"> 10881.70</td>
<td class="number"> 10923.60</td>
<td class="number"> 10807.10</td>
<td class="number"> 10910.10</td>
<td class="number"> 159404542</td>
<td class="number"> 8688.26</td>
</tr>
Python代码:
rows=soup.find_all("tr")
for row in rows:
cells=row.find_all('td')
print(cells)
输出:
[]
所需输出:
[01-Jan-2019,10881.70,10923.60,10807.10,10910.10,159404542,8688.26]
看起来可以使用css或语法指定多个选择器
就是这样
输出:
这是答案。 如果定义一个函数来检查两个类是否都存在,就可以解决这个问题。你知道吗
我不确定这是不是你想要的,但是我用了你的html,在营业额中插入了另一个日期
<th>
(我们有两个,看看是否两者都能被捕获),还有另一个<td>
和一个伪类,看看它是否能被排除。现在看起来是这样的:代码:
或者
然后:
输出:
相关问题 更多 >
编程相关推荐