Beautifulsoup HTML表格解析 - 只能获得最后一行?

时间:2016-08-03 20:42:11

标签: python html parsing beautifulsoup

我有一个简单的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。任何帮助是极大的赞赏!感谢

1 个答案:

答案 0 :(得分:0)

如果你想要一个列表中的所有标签,你需要扩展,你只需要重新分配cells = row.find_all('th')所以当你在循环外面的打印单元格时,你只能看到它最后分配的内容,即最后一个在最后一个tr:

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

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

soup = BeautifulSoup(html)

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

如果您想跳过thead行,可以使用 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