私は構文解析するための単純なHTMLテーブルを持っていますが、何とかBeautifulsoupは私に最後の行からの結果しか得られません。誰かがそれを見て、何が間違っているのかを知りたいと思っています。Beautifulsoup HTMLテーブルの解析 - 最後の行だけを取得できますか?
<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 & 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 & Biotechnology</td>
<td class="country">Portugal</td>
<td class="joined-on">2004-02-19</td>
</tr>]
を予想したように、それをだから私はすでに行がHTMLのテーブルからオブジェクトを作成します見える。しかし、私が続けると:
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'はどのように定義されていますか? – alecxe
ありがとう!表とコードについて詳しく説明します。 – AD233
それはあなたの求めることをまさにやっています。 'td'をすべて手に入れようとしていますか? –