2016-01-20 155 views
1

我在瀏覽其他許多問題後遇到困難。我的代碼目前正在將數據分解成命名行,但是返回的是整行代碼,而不僅僅是包含文本,我只是從以下行中尋找ASCO VALVE MFG。,INC。:我不知道如何才能拔出該行中的文本。Python和BeautifulSoup4 - 從TD標籤中提取文本

<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]對以配合第2個CSV文件。我希望這一切都有道理。

回答

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) 
+0

我不是確定如何將它們分配給一個變量。那你能給我什麼額外的指導嗎? – AlliDeacon

+0

@AlliDeacon我更新了我的答案! – gtlambert

+0

非常感謝!很抱歉,對於遲到的迴應,我嘗試了這種方法,但是這時數據並沒有分成適當的變量段。我會繼續努力,但如果您有更多指導,我們將一如既往地爲您效勞!感謝您提供的幫助。 – AlliDeacon