У меня есть эта таблица html: мне нужно получить конкретные данные из этой таблицы и назначить ее переменной, мне не нужна вся информация. flag = «Объединенные Арабские Эмираты», home_port = «Sharjah» и т. д. Поскольку на html-элементах нет «класса», как мы извлекаем эти данные.Таблица синтаксиса таблицы BeautifulSoup для тегов без классов
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>
Я использую модуль питона beautfulSoup. Не использовать какое-либо регулярное выражение. –