【发布时间】:2020-03-17 09:02:05
【问题描述】:
我有兴趣了解如何抓取网站。现在我学习如何在网站上刮桌子。我用的是 BeautifulSoup。
我有一个简单的 HTML 表来解析,但不知何故 Beautifulsoup 我试图在 tbody 中获取行但总是在“thead”中获取单词。 .我想知道是否有人会看一下,看看有什么问题。所以我已经从 HTML 表中创建了 rows 对象:
<table id="companyTable" class="table table--zebra table-content-page width-block dataTable no-footer" role="grid" aria-describedby="companyTable_info" style="width: 868px;">
<thead>
<tr role="row">
<th class="sorting_disabled" rowspan="1" colspan="1" style="width: 41px;">No</th>
<th class="sorting_disabled" rowspan="1" colspan="1" style="width: 224px;">Kode/Nama Perusahaan</th>
<th class="sorting_disabled" rowspan="1" colspan="1" style="width: 267px;">Nama</th>
<th class="sorting_disabled" rowspan="1" colspan="1" style="width: 187px;">Tanggal Pencatatan</th>
</tr>
</thead>
<tbody>
<tr role="row" class="odd">
<td class="text-center">1</td>
<td class="text-center">AALI</td>
<td><a href="/perusahaan-tercatat/profil-perusahaan-tercatat/detail-profile-perusahaan-tercatat/?kodeEmiten=AALI">Astra Agro Lestari Tbk</a></td>
<td>09 Des 1997</td>
</tr>
<tr role="row" class="even">
<td class="text-center">2</td>
<td class="text-center">ABBA</td>
<td><a href="/perusahaan-tercatat/profil-perusahaan-tercatat/detail-profile-perusahaan-tercatat/?kodeEmiten=ABBA">Mahaka Media Tbk</a></td>
<td>03 Apr 2002</td>
</tr>
我真的很抱歉我已经阅读并尝试了这个 Beautifulsoup HTML table parsing--only able to get the last row? 。但是,仍然没有得到它.. 并在输出中得到 '[ ]'。
这是我要抓取的链接。 :https://www.idx.co.id/perusahaan-tercatat/profil-perusahaan-tercatat/
我想得到这一行。
<tr role="row" class="odd">
<td class="text-center">1</td>
<td class="text-center">AALI</td>
<td><a href="/perusahaan-tercatat/profil-perusahaan-tercatat/detail-profile-perusahaan-tercatat/?kodeEmiten=AALI">Astra Agro Lestari Tbk</a></td>
<td>09 Des 1997</td>
</tr>
我试图得到它,但总是在“头”中得到消息。
这是我的代码:
from bs4 import BeautifulSoup as soup
from urllib.request import urlopen as uReq
url = 'https://www.idx.co.id/perusahaan-tercatat/profil-perusahaan-tercatat/'
uClient = uReq(url)
pageHtml = uClient.read()
uClient.close()
pageSoup = soup(pageHtml, "html.parser")
table = pageSoup.findAll('table', id = "companyTable")
table = table[0]
for row in table.findAll('tr'):
for cell in row.findAll('th'):
print(cell.text)
【问题讨论】:
标签: html python-3.x beautifulsoup