python - 使用 BeautifulSoup 通过 Python 从表中获取信息

标签 python beautifulsoup

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

我有一个 html 文档并使用

table = soup.findAll("table")

找到 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"的项目。

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

最佳答案

您可能会获取如下数据:

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']

关于python - 使用 BeautifulSoup 通过 Python 从表中获取信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32337952/

相关文章:

c++ - Python C++ 扩展 : compile only modified source files

python - 创建 Django 评论表单时出现 ValueError

python - 美汤提取

python - 调用命令并保留格式和数据类型

python - 如何使用 pandas 和 beautiful soup 来抓取多个网页地址上的表格?

python - 如何用Python绘制时间序列热图?

python - lxml: XMLSyntaxError: 不支持的版本 '2.0'

python - Tensorflow GradientDescentOptimizer - 它如何连接到 tf.Variables?

python - 使用 BeautifulSoup 确定脚本标签是否位于 head 或 body 中

python - BeautifulSoup - 向标签添加属性