Python和BeautifulSoup4 - 从TD标签中提取文本

时间:2016-01-20 14:16:01

标签: python beautifulsoup bs4

在查看了许多其他问题后,我陷入困境。我的代码目前正在将数据分解为命名行,但是返回的是整行而不仅仅是包含的文本,我只是从以下行查找ASCO VALVE MFG。,INC。:我不知道如何撤出行中的文字。

<td nowrap="nowrap" align="left"><font size="3" face="Arial,Helvetica,sans-serif">****ASCO VALVE MFG., INC.****</font></td>

我的输入如下: 头:

 <tr>
        <td align="center" id="ColHead_0"><font size="3" face="Arial,Helvetica,sans-serif"><b>WH</b></font></td>
        <td align="center" id="ColHead_1"><font size="3" face="Arial,Helvetica,sans-serif"><b>OrderNo.</b></font></td>
        <td align="center" id="ColHead_2"><font size="3" face="Arial,Helvetica,sans-serif"><b>Cust.</b></font></td>
        <td align="left" id="ColHead_3"><font size="3" face="Arial,Helvetica,sans-serif"><b>Customer Name</b></font></td>
        <td align="center" id="ColHead_4"><font size="3" face="Arial,Helvetica,sans-serif"><b>Item Number</b></font></td>
        <td align="center" id="ColHead_5"><font size="3" face="Arial,Helvetica,sans-serif"><b>Item Description 1</b></font></td>
        <td align="center" id="ColHead_6"><font size="3" face="Arial,Helvetica,sans-serif"><b>Item Description 2</b></font></td>
        <td align="center" id="ColHead_7"><font size="3" face="Arial,Helvetica,sans-serif"><b>Qty</b></font></td>
        <td align="center" id="ColHead_8"><font size="3" face="Arial,Helvetica,sans-serif"><b>S/N </b></font></td>
      </tr>

数据行如下:

<tr>
     <td nowrap="nowrap" align="left"><font size="3" face="Arial,Helvetica,sans-serif">09</font></td>
     <td nowrap="nowrap" align="left"><font size="3" face="Arial,Helvetica,sans-serif">92427</font></td>
     <td nowrap="nowrap" align="left"><font size="3" face="Arial,Helvetica,sans-serif">20668</font></td>
     <td nowrap="nowrap" align="left"><font size="3" face="Arial,Helvetica,sans-serif">ASCO VALVE MFG., INC.</font></td>
     <td nowrap="nowrap" align="left"><font size="3" face="Arial,Helvetica,sans-serif">EQPRAN77333</font></td>
     <td nowrap="nowrap" align="left"><font size="3" face="Arial,Helvetica,sans-serif">RANPAK FILLPAK TT</font></td>
     <td nowrap="nowrap" align="left"><font size="3" face="Arial,Helvetica,sans-serif">S/N 50742543</font></td>
     <td nowrap="nowrap" align="right"><font size="3" face="Arial,Helvetica,sans-serif">1</font></td>
     <td nowrap="nowrap" align="left"><font size="3" face="Arial,Helvetica,sans-serif">50742543</font></td>
</tr>

我的代码目前正在将数据分成命名行,但是返回整个html行。

soup1 = BeautifulSoup(output, "html.parser")
find_string = soup1.body.find_all(text="-")
Customer_No = []
Serial_No = []
rows = soup1.find_all("tr")
title = rows[0]
headers = rows[1]
datarows = rows[2:]

for row in datarows :
    if len(row)> 7:
        WHID = row.contents[1]
        ORNO = row.contents[3]
        CSNO = row.contents[5]
        CSNM = row.contents[7]
        ITNO = row.contents[9]
        DESC = row.contents[11]
        DESC2 = row.contents[13]
        QTY = row.contents[15]
        SN = row.contents[17]
        print ITNO
    else:
        continue

我想要最终得到的是一个词典,我猜[CSNO中的文本]和[SN中的文本]对以匹配第二个CSV文件。我希望一切都有道理。

1 个答案:

答案 0 :(得分:3)

您可以使用.text属性提取每个元素的文本。以下几行中的内容可以帮助您理解:

from bs4 import BeautifulSoup

content = '''
<tr>
    <td nowrap="nowrap" align="left"><font size="3" face="Arial,Helvetica,sans-serif">09</font></td>
    <td nowrap="nowrap" align="left"><font size="3" face="Arial,Helvetica,sans-serif">92427</font></td>
    <td nowrap="nowrap" align="left"><font size="3" face="Arial,Helvetica,sans-serif">20668</font></td>
    <td nowrap="nowrap" align="left"><font size="3" face="Arial,Helvetica,sans-serif">ASCO VALVE MFG., INC.</font></td>
    <td nowrap="nowrap" align="left"><font size="3" face="Arial,Helvetica,sans-serif">EQPRAN77333</font></td>
    <td nowrap="nowrap" align="left"><font size="3" face="Arial,Helvetica,sans-serif">RANPAK FILLPAK TT</font></td>
    <td nowrap="nowrap" align="left"><font size="3" face="Arial,Helvetica,sans-serif">S/N 50742543</font></td>
    <td nowrap="nowrap" align="right"><font size="3" face="Arial,Helvetica,sans-serif">1</font></td>
    <td nowrap="nowrap" align="left"><font size="3" face="Arial,Helvetica,sans-serif">50742543</font></td>
</tr>'''

soup = BeautifulSoup(content, 'html')
rows = soup.find_all('tr')
for row in rows:
    td_cells = soup.find_all('td')
    for td_cell in td_cells:
        print td_cell.text

<强>输出

09
92427
20668
ASCO VALVE MFG., INC.
EQPRAN77333
RANPAK FILLPAK TT
S/N 50742543
1
50742543

要存储文本,您可以执行以下操作:

soup = BeautifulSoup(content, 'html')
rows = soup.find_all('tr')
table_text = []
for row in rows:
    row_text = []
    td_cells = soup.find_all('td')
    for td_cell in td_cells:
        row_text.append(td_cell.text)
    table_text.append(row_text)