在beautifulsoup中查找具有类名的元素

时间:2017-12-09 20:24:41

标签: python beautifulsoup

soup = BeautifulSoup(open(filename), "lxml")
dd = {}

rows = soup.find_all('tr')

for r in rows:
    td = r.find_all('td')
    print(td)    

使用上面的代码,打印td会给我以下内容:

    [<td class="num cell-icon-string" data-sort-value="1"><i class="pki pkiAll n1" data-sprite="pkiAll n1"></i> 001</td>, <td class="cell-icon-string"><a class="ent-name" href="/pokedex/bulbasaur" title="View pokedex for #001 Bulbasaur">Bulbasaur</a></td>, <td class="cell-icon"><a class="type-icon type-grass" href="/type/grass">Grass</a><br/><a class="type-icon type-poison" href="/type/poison">Poison</a></td>, <td class="num-total">318</td>, <td class="num">45</td>, <td class="num">49</td>, <td class="num">49</td>, <td class="num">65</td>, <td class="num">65</td>, <td class="num">45</td>]
    [<td class="num cell-icon-string" data-sort-value="2"><i class="pki pkiAll n2" data-sprite="pkiAll n2"></i> 002</td>, <td class="cell-icon-string"><a class="ent-name" href="/pokedex/ivysaur" title="View pokedex for #002 Ivysaur">Ivysaur</a></td>, <td class="cell-icon"><a class="type-icon type-grass" href="/type/grass">Grass</a><br/><a class="type-icon type-poison" href="/type/poison">Poison</a></td>, <td class="num-total">405</td>, <td class="num">60</td>, <td class="num">62</td>, <td class="num">63</td>, <td class="num">80</td>, <td class="num">80</td>, <td class="num">60</td>]

从这个td,我特别想获得类型名称和名称:

<a class="type-icon type-grass" href="/type/grass">Grass</a><br/>

<a class="ent-name" href="/pokedex/bulbasaur" title="View pokedex for #001 Bulbasaur">Bulbasaur</a></td>

但我很难访问这些特定元素。

我怎么能用beautifulsoup做到这一点?

0 个答案:

没有答案