2013-11-28 61 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」:變量}) –