我有這個HTML表:我需要從這個表中獲取特定數據並將其分配給一個變量,我不需要所有的信息。 flag =「阿拉伯聯合酋長國」,home_port =「Sharjah」等。由於html元素沒有'class',我們如何提取這些數據。BeautifulSoup HTML表分析爲無標記的標記
r = requests.get('http://maritime-connector.com/ship/'+str(imo_number), headers={'User-Agent': 'Mozilla/5.0'})
soup = BeautifulSoup(r.content, "lxml")
table = soup.find("table", { "class" : "ship-data-table" })
for row in table.findAll("tr"):
tname = row.findAll("th")
cells = row.findAll("td")
print (type(tname))
print (type(cells))
我使用python模塊beautfulSoup。
<table class="ship-data-table" style="margin-bottom:3px">
<thead>
<tr>
<th>IMO number</th>
<td>9492749</td>
</tr>
<tr>
<th>Name of the ship</th>
<td>SHARIEF PILOT</td>
</tr>
<tr>
<th>Type of ship</th>
<td>ANCHOR HANDLING VESSEL</td>
</tr>
<tr>
<th>MMSI</th>
<td>470535000</td>
</tr>
<tr>
<th>Gross tonnage</th>
<td>499 tons</td>
</tr>
<tr>
<th>DWT</th>
<td>222 tons</td>
</tr>
<tr>
<th>Year of build</th>
<td>2008</td>
</tr>
<tr>
<th>Builder</th>
<td>NANYANG SHIPBUILDING - JINGJIANG, CHINA</td>
</tr>
<tr>
<th>Flag</th>
<td>UNITED ARAB EMIRATES</td>
</tr>
<tr>
<th>Home port</th>
<td>SHARJAH</td>
</tr>
<tr>
<th>Manager & owner</th>
<td>GLOBAL MARINE SERVICES - SHARJAH, UNITED ARAB EMIRATES</td>
</tr>
<tr>
<th>Former names</th>
<td>SUPERIOR PILOT until 2008 Sep</td>
</tr>
</thead>
</table>
內容我使用Python模塊beautfulSoup。不使用任何正則表達式。 –