【问题标题】:BeautifulSoup HTML scraping, how to get row after thead in tbodyBeautifulSoup HTML抓取,如何在tbody中的thead之后获取行
【发布时间】: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


    【解决方案1】:

    解决问题

    如果我没听错的话,你只是想从this site 获取表格数据。然而,检查站点并使用Google Network tools 分析请求和响应,我刚刚发现该站点正在使用DataTables 并使用JS 填充表格,并使用来自this request 的响应。

    换句话说,你可以做到

    import requests
    
    url = "https://www.idx.co.id/umbraco/Surface/Helper/GetEmiten?emitenType=s"
    response = requests.get(url)
    print(response.json())
    

    你应该从中学到什么

    检查页面元素和请求/响应,以了解获取数据的最简单方法。我建议的工具是Chrome Devtools,但您可以使用最适合您的浏览器。

    【讨论】:

    • 感谢您的回复。但我认为我犯了一个需要抓取的错误,但我写了“解析”。我很抱歉.. :( 但是,我也很想学习解析 :)
    • 您可以重写您的问题,以便我们为您提供帮助。
    • 您可以在返回的 json 上使用 pd.DataFrame 但请注意,此 json 仅返回两列并且错过了最后一列。
    • 可能剩余的数据在另一个请求中。但这里的重点确实是:他要搜索的数据不是在返回的页面上,而是在另一个请求上。
    【解决方案2】:

    您只需要tbody 标记中的第一个tr。所以我会用这个:

    first_row = s.find('tbody').find('tr')
    

    s 是我的汤。这是一个例子:

    >>> html = """<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>
    ... """
    >>> s = BeautifulSoup(html)
    >>> first_row = s.find('tbody').find('tr')
    >>> first_row
    <tr class="odd" role="row">
    <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>
    

    之所以有效,是因为find 只返回第一个匹配的元素

    【讨论】:

      猜你喜欢
      • 2020-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-07
      相关资源
      最近更新 更多