我是Python和BeautifulSoup的初學者,我試圖製作一個web刮板。但是,我面臨着一些問題,無法找出出路。這是我的問題:如何在以下示例中使用BeauifulSoup解析數據?
這是我想要放棄的HTML的一部分:
<tr>
<td class="num cell-icon-string" data-sort-value="6">
<td class="cell-icon-string"><a class="ent-name" href="/pokedex/charizard" title="View pokedex for #006 Charizard">Charizard</a></td>
</tr>
<tr>
<td class="num cell-icon-string" data-sort-value="6">
<td class="cell-icon-string"><a class="ent-name" href="/pokedex/charizard" title="View pokedex for #006 Charizard">Charizard</a><br>
<small class="aside">Mega Charizard X</small></td>
</tr>
現在,我想從1號錶行和「超級噴火龍X」提取「噴火龍」第二排。現在,我可以從兩行中提取「Charizard」。
這裏是我的代碼:
#!/usr/bin/env python3
from bs4 import BeautifulSoup
soup = BeautifulSoup(open("data.html"), "lxml")
poke_boxes = soup.findAll('a', attrs = {'class': 'ent-name'})
for poke_box in poke_boxes:
poke_name = poke_box.text.strip()
print(poke_name)
謝謝s!我瞭解你的邏輯並採用一些解決方法,能夠實現所需的事情。 – torque