2013-11-28 60 views
2

我试图嵌套在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> 

我有提取的统计数据,这是嵌套在TD标签的数据sortval之外的问题。我想我想要拉所有的tr标签,但我不知道如何从包含td标签的data-sortval =“Ahri”拉出tr标签。在那一点上,我想通过tr标签遍历x次,直到达到我想要的第一个统计信息,10.98

此刻,我正在尝试为数据排序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}) 

这证实了一个是一个字符串。 但它会抛出此错误:

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

回答

4

GO LOL!

出于商业目的,请仔细阅读刮刮前的服务条款。

(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% 
... 
+1

太谢谢你了! 这实际上是为了研究目的,因为我是我大学的学生研究员。 我希望免费发布它,如果可能的话,但我一定会按照你的建议去做,并阅读服务条款。 – Noc

+0

我有一个问题,但。我如何设法改变 soup.find('td',{“data-sortval”:“Ahri”}) 使用变量来代替“Ahri”,让我们说一个字典的所有关键字?目前,我将键值转换为字符串,然后尝试将它们传递给for循环,但似乎发现不会使用可变标题 – Noc

+0

soup.find(“td”,{“data-sortval”:变量}) –