【问题标题】:Beautifulsoup HTML table parsing--only able to get the last row?Beautifulsoup HTML 表格解析——只能得到最后一行?
【发布时间】:2016-12-09 17:52:42
【问题描述】:

我有一个简单的 HTML 表来解析,但不知何故,Beautifulsoup 只能从最后一行得到结果。我想知道是否有人会看一下,看看有什么问题。所以我已经从 HTML 表中创建了 rows 对象:

 <table class='participants-table'>
    <thead>
      <tr>
          <th data-field="name" class="sort-direction-toggle name">Name</th>
          <th data-field="type" class="sort-direction-toggle type active-sort asc">Type</th>
          <th data-field="sector" class="sort-direction-toggle sector">Sector</th>
          <th data-field="country" class="sort-direction-toggle country">Country</th>
          <th data-field="joined_on" class="sort-direction-toggle joined-on">Joined On</th>
      </tr>
    </thead>
    <tbody>
        <tr>
          <th class='name'><a href="/what-is-gc/participants/4479-Grontmij">Grontmij</a></th>
          <td class='type'>Company</td>
          <td class='sector'>General Industrials</td>
          <td class='country'>Netherlands</td>
          <td class='joined-on'>2000-09-20</td>
        </tr>
        <tr>
          <th class='name'><a href="/what-is-gc/participants/4492-Groupe-Bial">Groupe Bial</a></th>
          <td class='type'>Company</td>
          <td class='sector'>Pharmaceuticals &amp; Biotechnology</td>
          <td class='country'>Portugal</td>
          <td class='joined-on'>2004-02-19</td>
        </tr>
    </tbody>
  </table>

我使用以下代码获取行:

table=soup.find_all("table", class_="participants-table")
table1=table[0]
rows=table1.find_all('tr')
rows=rows[1:]

这得到:

rows=[<tr>
 <th class="name"><a href="/what-is-gc/participants/4479-Grontmij">Grontmij</a></th>
 <td class="type">Company</td>
 <td class="sector">General Industrials</td>
 <td class="country">Netherlands</td>
 <td class="joined-on">2000-09-20</td>
 </tr>, <tr>
 <th class="name"><a href="/what-is-gc/participants/4492-Groupe-Bial">Groupe Bial</a></th>
 <td class="type">Company</td>
 <td class="sector">Pharmaceuticals &amp; Biotechnology</td>
 <td class="country">Portugal</td>
 <td class="joined-on">2004-02-19</td>
 </tr>]

正如预期的那样,它看起来像。但是,如果我继续:

for row in rows:
    cells = row.find_all('th')

我只能得到最后一个条目!

cells=[<th class="name"><a href="/what-is-gc/participants/4492-Groupe-Bial">Groupe Bial</a></th>]

发生了什么事?这是我第一次使用beautifulsoup,我想做的是将这个表导出为CSV。任何帮助是极大的赞赏!谢谢

【问题讨论】:

  • rows 是如何定义的?
  • 谢谢!提供了有关表格和代码的更多详细信息。
  • 它正在做你要求它做的事情。您是否要获取所有 td 的信息?

标签: python html parsing beautifulsoup


【解决方案1】:

如果您想将所有 th 标签放在一个列表中,则需要扩展,您只需不断重新分配cells = row.find_all('th'),因此当您在循环外打印单元格时,您只会看到它最后分配给的内容,即最后一个 th 在最后一次:

cells = []
for row in rows:
 cells.extend(row.find_all('th'))

此外,由于只有一张表,您可以使用 find

soup = BeautifulSoup(html)

table = soup.find("table", class_="participants-table")

如果您想跳过第一行,您可以使用 css 选择器

from bs4 import BeautifulSoup

soup = BeautifulSoup(html)

rows = soup.select("table.participants-table  thead ~ tr")

cells = [tr.th for tr in rows]
print(cells)

细胞会给你:

[<th class="name"><a href="/what-is-gc/participants/4479-Grontmij">Grontmij</a></th>, <th class="name"><a href="/what-is-gc/participants/4492-Groupe-Bial">Groupe Bial</a></th>]

要将整个表格写入 csv:

import csv

soup = BeautifulSoup(html, "html.parser")

rows = soup.select("table.participants-table tr")

with open("data.csv", "w") as out:
    wr = csv.writer(out)
    wr.writerow([th.text for th in rows[0].find_all("th")] + ["URL"])

    for row in rows[1:]:
        wr.writerow([tag.text for tag in row.find_all()] + [row.th.a["href"]])

你的样品会给你:

Name,Type,Sector,Country,Joined On,URL
Grontmij,Company,General Industrials,Netherlands,2000-09-20,/what-is-gc/participants/4479-Grontmij
Groupe Bial,Company,Pharmaceuticals & Biotechnology,Portugal,2004-02-19,/what-is-gc/participants/4492-Groupe-Bial

【讨论】:

  • 谢谢!这使它离我的目标更近了!实际上,我想做的是将此表导出为典型的 CSV 格式,并将“名称”和 html 链接作为单独的列。有没有办法用你刚才建议的“扩展”方法来做到这一点?谢谢!
  • @AD233,所以你基本上想在 csv 中重新创建表?
  • 这是正确的,除了我想将 href 链接提取为单​​独的列。谢谢!
  • 太棒了!谢谢您的帮助!这确实很有趣
猜你喜欢
  • 2011-01-04
  • 2014-09-03
  • 2011-05-10
  • 1970-01-01
  • 1970-01-01
  • 2011-09-24
  • 2012-01-12
  • 1970-01-01
  • 2012-10-01
相关资源
最近更新 更多