使用BeautifulSoup从Python获取表格信息

时间:2015-09-01 17:49:42

标签: python beautifulsoup

我安装了BeautifulSoup,阅读了文档并找到了一些关于从表中获取信息的教程,但只从基本表中添加了几行和列。 我无法理解如何做更复杂的事情。

我有一个HTML文档并使用

table = soup.findAll("table")

找到桌子。我得到的是一堆看起来像这样的列表:

<tbody class="item" data-buyout="8 exalted" data-ign="POOPOODOODOO" data-league="Standard" 
data-name="Dusk Stone Cobalt Jewel" data-seller="LooseSausage" data-sellerid="3447078" 
data-thread="1285903" id="item-container-0"> 
<tr class="first-line"> <td class="icon-td"> <div class="icon"><img alt="Item icon" src="http://webcdn.pathofexile.com/image/Art/2DItems/Jewels/basicint.png?scale=1&amp;w=1&amp;h=1&amp;v=cd579ea22c05f1c6ad2fd015d7a710bd3">\n<div class="sockets" style="position: absolute;">

\n<div class="sockets-inner" style="position: relative; width:94px;">\n</div>\n</div></img></div> </td> 
<td class="item-cell"> 

<h5><a class="title itemframe2" href="http://www.pathofexile.com/forum/view-thread/1285903" target="_blank">  Dusk Stone Cobalt Jewel </a></h5><p class="requirements"> </p> <span class="sockets-raw" style="display:none"></span> <ul class="item-mods"><li class="bullet-item"><ul class="mods">

<li class="sortable " data-name="##% increased Attack Speed" data-value="4.0" style=""><b>4</b>% increased Attack Speed</li>
<li class="sortable " data-name="#Minions have #% increased maximum Life" data-value="10.0" style="">Minions have <b>10</b>% increased maximum Life</li>
<li class="sortable " data-name="##% increased Ignite Duration on Enemies" data-value="5.0" style=""><b>5</b>% increased Ignite Duration on Enemies</li><li class="sortable active" data-name="#Minions deal #% increased Damage" data-value="16.0" style="">Minions deal <b>16</b>% increased Damage</li>
<li class="sortable " data-name="##% chance to Ignite" data-value="2.0" style=""><b>2</b>% chance to Ignite</li></ul></li></ul> </td> <td class="table-stats"> <table> <tr class="calibrate"> <th></th> <th></th> <th></th> <th></th> <th></th> <th></th> <th></th> <th></th> <th></th> <th></th> <th></th> <th></th> <th></th> <th></th> </tr> <tr class="cell-first"> <th class="disabled" colspan="2">Quality</th> <th class="disabled" colspan="2">Phys.</th> <th class="disabled" colspan="2">Elem.</th> <th class="disabled" colspan="2">APS</th> <th class="disabled" colspan="2">DPS</th> <th class="disabled" colspan="2">pDPS</th> <th class="disabled" colspan="2">eDPS</th> </tr> <tr class="cell-first"> <td class="sortable property " colspan="2" data-name="q" data-value="0">  0<span class="capped">+20</span> </td> <td class="sortable property " colspan="2" data-name="quality_pd" data-value="0.0"> </td> <td class="sortable property " colspan="2" data-ed="" data-name="ed" data-value="0.0"> </td> <td class="sortable property " colspan="2" data-name="aps" data-value="0">  \xa0  </td> <td class="sortable property " colspan="2" data-name="quality_dps" data-value="0.0">  \xa0  </td> <td class="sortable property " colspan="2" data-name="quality_pdps" data-value="0.0">  \xa0  </td> <td class="sortable property " colspan="2" data-name="edps" data-value="0.0">  \xa0  </td> </tr> <tr class="cell-second"> <th class="cell-empty"></th> <th class="disabled" colspan="2">Armour</th> <th class="disabled" colspan="2">Evasion</th> <th class="disabled" colspan="2">Shield</th> <th class="disabled" colspan="2">Block</th> <th class="disabled" colspan="2">Crit.</th> <th class="disabled" colspan="2">Level</th> </tr> <tr class="cell-second"> <td class="cell-empty"></td> <td class="sortable property " colspan="2" data-name="quality_armour" data-value="0.0">  \xa0  </td> <td class="sortable property " colspan="2" data-name="quality_evasion" data-value="0.0">  \xa0  </td> <td class="sortable property " colspan="2" data-name="quality_shield" data-value="0.0">  \xa0  </td> <td class="sortable property " colspan="2" data-name="block" data-value="0">  \xa0  </td> <td class="sortable property " colspan="2" data-name="crit" data-value="0">  \xa0  </td> <td class="sortable property " colspan="2" data-name="level" data-value="0">  \xa0  </td> </tr> </table> </td> </tr> <tr class="bottom-row"> <td class="first-cell"></td> <td> <span class="requirements"> <span class="sortable" data-name="price_in_chaos" data-value="-212.0"><span class="has-tip currency currency-exalted" data-tooltip="" title="8 exalted">8\xd7</span></span> \xb7   <span class="click-button" data-hash="61053af5f4c3a82e330b3e38192b8480" data-thread="1285903" onclick="verify_modern(this)">Verify</span>   \xb7 <span class="success label">online</span> IGN: POOPOODOODOO   \xb7 <span class="has-tip" data-tooltip="" title="account age and highest level">a857h93</span>    \xb7 <a href="#" onclick="sendPM(this);return false">PM</a>    \xb7 <a href="#" onclick="sendWhisper(this);return false">Whisper</a> </span> </td> <td class="third-cell" colspan="16"></td> </tr> <tr><td class="item-separator" colspan="16"></td></tr> 
</tbody> 

我想做的是采用数据买断,数据点击,数据名称等部分,并将它们保存在变量中以供日后使用。然后跳到li class =“sortable”并获取数据名称部分或文本。 (因为它们似乎是一样的)

我无法理解如何在tbody class =“item”部分内完成所有操作。

我需要多次这样做,因为每个表中有多个带有tbody class =“item”的项目。

非常感谢我能得到的任何信息!

1 个答案:

答案 0 :(得分:1)

您可以获得如下数据:

from bs4 import BeautifulSoup

soup = BeautifulSoup(your_string_above)
>>>soup.tbody.attrs
{'data-sellerid': '3447078', 'data-buyout': '8 exalted', 'data-league': 'Standard', 'data-name': 'Dusk Stone Cobalt Jewel', 'data-thread': '1285903', 'id': 'item-container-0', 'data-ign': 'POOPOODOODOO', 'data-seller': 'LooseSausage', 'class': ['item']}

>>>[x.get_text(strip=True) for x in soup.select('li')]
['4% increased Attack SpeedMinions have10% increased maximum Life5% increased Ignite Duration on EnemiesMinions deal16% increased Damage2% chance to Ignite', '4% increased Attack Speed', 'Minions have10% increased maximum Life', '5% increased Ignite Duration on Enemies', 'Minions deal16% increased Damage', '2% chance to Ignite']