gpt4 book ai didi

python - 如何解析div中的表格

转载 作者:行者123 更新时间:2023-12-05 05:22:54 25 4
gpt4 key购买 nike

<分区>

                                <div id="findet_1" name="findet_1" >

<table width="100%" border="0" cellspacing="0" cellpadding="0">

<tr>

<td class="thc01 w160 gL_10 UC" >&nbsp;Standalone</td>

<td class="thc01 w160 gL_10 tar">Jun'16</td>

<td class="thc01 w160 gL_10 tar">Mar'16</td>

<td class="thc01 w160 gL_10 tar">Dec'15</td>

<td class="thc01 w160 gL_10 tar"><div class="PR20">Sep'15</div></td>

</tr>

<tr>

<td class="thc02 w160 gD_12" >Net Sales</td>

<td class="thc02 w160 gD_12 tar">16,339.70</td>

<td class="thc02 w160 gD_12 tar">15,589.40</td>

<td class="thc02 w160 gD_12 tar">15,065.00</td>

<td class="thc02 w160 gD_12 tar"><span class="PR20">14,824.50</span></td>

</tr>

<tr>

<td class="thc02 w160 gD_12" >Other Income</td>

<td class="thc02 w160 gD_12 tar">50.10</td>

<td class="thc02 w160 gD_12 tar">46.30</td>

<td class="thc02 w160 gD_12 tar">153.30</td>

<td class="thc02 w160 gD_12 tar"><span class="PR20">1,087.40</span></td>

</tr>

<tr>

<td class="thc02 w160 gD_12" >PBDIT</td>

<td class="thc02 w160 gD_12 tar">6,612.30</td>

<td class="thc02 w160 gD_12 tar">5,930.60</td>

<td class="thc02 w160 gD_12 tar">5,543.30</td>

<td class="thc02 w160 gD_12 tar"><span class="PR20">5,416.80</span></td>

</tr>

<tr>

<td class="thc02 w160 gD_12" >Net Profit</td>

<td class="thc02 w160 gD_12 tar">1,427.50</td>

<td class="thc02 w160 gD_12 tar">1,693.90</td>

<td class="thc02 w160 gD_12 tar">1,709.10</td>

<td class="thc02 w160 gD_12 tar"><span class="PR20">2,223.70</span></td>

</tr>

</table>

</div>

我正在尝试阅读这张表格。但无法这样做。我首先使用 beautyfulsoup findall 来查找 div。表格存在于 div 中。我找不到那张 table 。第二个问题是遍历行的最佳方式是什么。例如,我想要 csv 格式的输出,应该用双引号括起来像 :“独立”,“Jun'16”,“Mar'16”,“Dec'15”,“Sep'15”“净销售额”、“16,339.70”、“15,589.40”、“15,065.00”、“14,824.50”“其他收入”、“50.10”、“46.30”、“153.30”、“1,087.40”"PBDIT","6,612.30","5,930.60","5,543.30","5,416.80""净利润","1,427.50","1,693.90","1,709.10","2,223.70"

我的代码:

从 urllib.request 导入 urlopen从 bs4 导入 BeautifulSoup导入重新

html = urlopen("http://www.moneycontrol.com/india/stockpricequote/computers-software/tataconsultancyservices/TCS")
bsObj = BeautifulSoup(html, "html.parser")
link = bsObj.findAll("div", id="findet_1")
table1 = link.find('table').find_all('tr')

我知道我们可以使用 get_text 获取值并使用 for 循环遍历行。但我找不到表:(

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com