gpt4 book ai didi

python - table 和漂亮汤的问题

转载 作者:行者123 更新时间:2023-11-28 22:52:39 25 4
gpt4 key购买 nike

我正在尝试嵌套在 tr 标签中的标签,但我用来查找正确值的标识符嵌套在 tr 标签内的另一个 td 中。

也就是说,我正在使用网站 LoLKing

并尝试根据名称(例如 Ahri)对其进行抓取以获取统计信息。

HTML 是:

<tr>
<td data-sorttype="string" data-sortval="Ahri" style="text-align: left;">
<div style="display: table-cell;">
<div class="champion-list-icon" style="background:url(//lkimg.zamimg.com/shared/riot/images/champions/103_32.png)">
<a style="display: inline-block; width: 28px; height: 28px;" href="/champions/ahri"></a>
</div>
</div>
<div style="display: table-cell; vertical-align: middle; padding-top: 3px; padding-left: 5px;"><a href="/champions/ahri">Ahri</a></div>
</td>
<td style="text-align: center;" data-sortval="975"><img src='//lkimg.zamimg.com/images/rp_logo.png' width='18' class='champion-price-icon'>975</td>
<td style="text-align: center;" data-sortval="6300"><img src='//lkimg.zamimg.com/images/ip_logo.png' width='18' class='champion-price-icon'>6300</td>
<td style="text-align: center;" data-sortval="10.98">10.98%</td>
<td style="text-align: center;" data-sortval="48.44">48.44%</td>
<td style="text-align: center;" data-sortval="18.85">18.85%</td>
<td style="text-align: center;" data-sorttype="string" data-sortval="Middle Lane">Middle Lane</td>
<td style="text-align: center;" data-sortval="1323849600">12/14/2011</td>
</tr>

我在提取统计信息时遇到问题,这些统计信息嵌套在 data-sortval 之外的 td 标签中。我想我想提取所有 tr 标签,但我不知道如何根据包含 data-sortval="Ahri"的 td 标签提取 tr 标签。到那时,我想遍历 tr 标记 x 次,直到达到我想要的第一个统计数据,10.98

目前,我正在尝试使用 data-sortval Ahri 查找 td,但它不会返回 tr 的其余部分。

如果一个较大的标签,不要将所有这些都嵌套在里面可能很重要:

  <table class="clientsort champion-list" width="100%" cellspacing="0" cellpadding="0">
<thead>
<tr><th>Champion</th><th>RP Cost</th><th>IP Cost</th><th>Popularity</th><th>Win Rate</th><th>Ban Rate</th><th>Meta</th><th>Released</th></tr>
</thead>
<tbody>

对于不够清晰,我深表歉意,我是这个抓取术语的新手,但我希望这足够有意义。现在,我也在做:

main = soup.find('table', {'class':'clientsort champion-list'})

只获取那个表

编辑:

我为变量输入了这个:

for champ in champs:
a = str(champ)
print type(a) is str
td_name = soup.find('td',{"data-sortval":a})

它确认 a 是一个字符串。但它抛出这个错误:

  File "lolrec.py", line 82, in StatScrape
tr = td_name.parent
AttributeError: 'NoneType' object has no attribute 'parent'

最佳答案

加油!

出于商业目的,请在抓取前阅读服务条款。

(1) 要抓取英雄列表,您可以执行此操作,这遵循与您描述的类似的逻辑。

from bs4 import BeautifulSoup
import urllib2
html = urllib2.urlopen('http://www.lolking.net/champions/')
soup = BeautifulSoup(html)
# locate the cell that contains hero name: Ahri
hero_list = ["Blitzcrank", "Ahri", "Akali"]
for hero in hero_list:
td_name = soup.find('td', {"data-sortval":hero})
tr = td_name.parent
popularity = tr.find_all('td', recursive=False)[3].text
print hero, popularity

输出

Blitzcrank 12.58%
Ahri 10.98%
Akali 7.52%

输出

10.98%

(2) 把英雄都刮了。

from bs4 import BeautifulSoup
import urllib2
html = urllib2.urlopen('http://www.lolking.net/champions/')
soup = BeautifulSoup(html)
# find the table first
table = soup.find('table', {"class":"clientsort champion-list"})
# find the all the rows
for row in table.find('tbody').find_all("tr", recursive=False):
cols = row.find_all("td")
hero = cols[0].text.strip()
popularity = cols[3].text
print hero, popularity

输出:

Aatrox 6.86%
Ahri 10.98%
Akali 7.52%
Alistar 4.9%
Amumu 8.75%
...

关于python - table 和漂亮汤的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20255553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com