2016-12-17 38 views
1

我是BeautifulSoup的新手。我试图从ESPN Fantasy Basketball Standings刮掉“季节统计”表格,但并不是所有的行都被返回。经过一番研究,我认为它可能是html.parser的一个问题,所以我使用了lxml。我得到了同样的结果。如果有人能告诉我如何获得所有球队的名字,我将不胜感激。Python - 用BeautifulSoup刮没有显示所有行

我的代码:

from bs4 import BeautifulSoup 
from urllib.request import urlopen 

soup = BeautifulSoup(urlopen("http://games.espn.com/fba/standings?leagueId=20960&seasonId=2017"),'html.parser') 
tableStats = soup.find("table", {"class" : "tableBody"}) 
for row in tableStats.findAll('tr')[2:]: 
    col = row.findAll('td') 

    try: 
     name = col[0].a.string.strip() 
     print(name) 
    except Exception as e: 
     print(str(e)) 

输出(你可以看到,显示只有少数球队的名字):

Le Tuc Grizzlies Peyton Ravens Heaven Vultures Versailles Golden Bears Baltimore Corto's La Murette Scavengers XO Gayfishes

+0

你似乎是采取错误的表格。为什么不采取总排名部分? – martianwars

回答

1

你似乎完全走错table。代替为<table>标签运行find(),您可以使用findAll()代替,并查找具有完整排名的正确表。另外我注意到统计表有一个叫做statsTable的特殊表id。寻找这个id而不是class是个好主意,因为它对于HTML文件是唯一的。

看一看在更多的指引下面代码中的注释,

from bs4 import BeautifulSoup 
import requests 
# Note, I'm using requests here as it's a superior library 
text = requests.get("http://games.espn.com/fba/standings?leagueId=20960&seasonId=2017").text 
soup = BeautifulSoup(text,'html.parser') 
# searching by id, always a better option when available 
tableStats = soup.find("table", {"id" : "statsTable"}) 
for row in tableStats.findAll('tr')[3:]: 
    col = row.findAll('td') 
    try: 
     # This fetches all the text in the tag stripped off all the HTML 
     name = col[1].get_text() 
     print(name) 
    except Exception as e: 
     print(str(e)) 
0

它可能更容易解析id="statsTable",其中包含所有的球队,即:

from bs4 import BeautifulSoup 
from urllib2 import urlopen 
soup = BeautifulSoup(urlopen("http://games.espn.com/fba/standings?leagueId=20960&seasonId=2017"),'html.parser') 
tableStats = soup.find('table', id="statsTable") 
for row in tableStats.findAll('a', href=True): 
    print row.text