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文件。我希望這一切都有道理。
我不是確定如何將它們分配給一個變量。那你能給我什麼額外的指導嗎? – AlliDeacon
@AlliDeacon我更新了我的答案! – gtlambert
非常感謝!很抱歉,對於遲到的迴應,我嘗試了這種方法,但是這時數據並沒有分成適當的變量段。我會繼續努力,但如果您有更多指導,我們將一如既往地爲您效勞!感謝您提供的幫助。 – AlliDeacon