【问题标题】:Write a consequence of children tags in one line into csv file using BeautifulSoup使用 BeautifulSoup 将一行中子标签的结果写入 csv 文件
【发布时间】:2016-09-02 10:54:10
【问题描述】:
from bs4 import BeautifulSoup
import urllib2
import re
import json
p = """
<thead>
<tr>
<th>Company Name</th>
<th>Symbol</th>
<th>Market</th>
<th>Price</th>
<th>Shares</th>
<th>Offer Amount</th>
<th>Date Priced</th>
</tr>
</thead>
<tr>
<td><a href="http://www.nasdaq.com" id="two">EXFO INC.</a></td>
<td><a href="http://www.nasdaq.com" id="two">EXFO</a></td>
<td><a href="http://www.nasdaq.com" id="two">NASDAQ</a></td>
<td>$26</td>
<td>7,000,000</td>
<td>$182,000,000</td>
<td>6/30/2000</td>
</tr>
<tr>
<td><a href="http://www.nasdaq.com">IGO, INC.</a></td>
<td><a href="http://www.nasdaq.com" id="two">MOBE</a></td>
<td><a href="http://www.nasdaq.com" id="two">NASDAQ</a></td>
<td>$12</td>
<td>4,000,000</td>
<td>$48,000,000</td>
<td>6/30/2000</td>
</tr>"""
soup = BeautifulSoup(p, 'html.parser')
for ana in soup.find_all('td'):
    if ana.parent.name == 'tr':
    print ana.string

嗨!我正在尝试将来自一个站点的一些数据写入 csv 文件。理想的结果是一个带有

的csv文件
EXFO INC.,EXFO,NASDAQ,$26,7,000,000,$182,000,000,6/30/2000
IGO, INC.,MOBE,NASDAQ, $12, 4,000,000,$48,000,000,6/30/2000

我现在学会做的是打印以下内容

EXFO INC.
EXFO
NASDAQ
$26
7,000,000
$182,000,000
6/30/2000
IGO, INC.
MOBE
NASDAQ
$12
4,000,000
$48,000,000
6/30/2000

任何想法如何做到这一点?我只是不知道如何将它全部放入循环中并为每个标签“”提取所有“”标签。

【问题讨论】:

  • 我的意思是为每个标签 tr 提取所有的 td 标签。

标签: csv tags beautifulsoup


【解决方案1】:

选择表格,在 thead 中找到 th 标签并写入,然后提取所有其他行并写入 td 文本:

from bs4 import BeautifulSoup
from csv import writer

soup = BeautifulSoup(html)
table = soup.select_one("table")
with open("out.csv", "w") as f:
    wr = writer(f)
    wr.writerow([th.text for th in table.select("thead  th")])
    for row in table.find_all("tr"):
        data = [td.text for td in row.find_all("td")]
        if data:
            wr.writerow(data)

这会给你:

Company Name,Symbol,Market,Price,Shares,Offer Amount,Date Priced
EXFO INC.,EXFO,NASDAQ,$26,"7,000,000","$182,000,000",6/30/2000
"IGO, INC.",MOBE,NASDAQ,$12,"4,000,000","$48,000,000",6/30/2000

另一种方法是找到所有 tr's 和索引/切片:

from bs4 import BeautifulSoup
from csv import writer
soup = BeautifulSoup(html)

rows = soup.select("table tr")
with open("out.csv", "w") as f:
    wr = writer(f)
    wr.writerow([th.text for th in rows[0].find_all("th")])
    for row in rows[1:]:
        data = [td.text for td in row.find_all("td")]
        wr.writerow(data)

无论采用何种方法,您都希望遍历所有 tr 标签,以便提取每个 tr 内所有相关的 td 标签,从而将数据分组到行中。

【讨论】:

  • 感谢您的广泛回答。它对我帮助很大!
猜你喜欢
  • 1970-01-01
  • 2013-09-24
  • 1970-01-01
  • 1970-01-01
  • 2017-06-26
  • 2015-09-22
  • 2021-01-12
  • 2014-10-06
  • 2011-09-30
相关资源
最近更新 更多