2016-04-07 21 views
0

Python ve web kazımaya yeni. Bazı analizler için http://www.basketball-reference.com/awards/all_league.html'u kazımak ve sadece şu ana kadar kazandım. Aşağıdaki kodla, yalnızca 3 satırı kazıyorum ve yıl atanırken 'liste dizini aralık dışı' hatası alıyorum. Herhangi bir yardım/ipuçları takdir edildi. Boş listenin elemanını 0 elde etmeye çalışmak, böylecePython 2.7 web kazıma - indeks dışı liste endeksi

r = requests.get('http://www.basketball-reference.com/awards/all_league.html') 
soup=BeautifulSoup(r.text.replace(' ','').replace('>','').encode('ascii','ignore'),"html.parser") 
all_league_data = pd.DataFrame(columns = ['year','team','player']) 


stw_list = soup.findAll('div', attrs={'class': 'stw'}) # Find all 'stw's' 
for stw in stw_list: 
    table = stw.find('table', attrs = {'class':'no_highlight stats_table'}) 
    for row in table.findAll('tr'): 
     col = row.findAll('td') 
     year = col[0].find(text=True) 
     print year 

cevap

0

satır bazıları td yok.

yapın:

r = requests.get('http://www.basketball-reference.com/awards/all_league.html') 
soup=BeautifulSoup(r.text.replace(' ','').replace('>','').encode('ascii','ignore'),"html.parser") 
all_league_data = pd.DataFrame(columns = ['year','team','player']) 

stw_list = soup.findAll('div', attrs={'class': 'stw'}) # Find all 'stw's' 
for stw in stw_list: 
    table = stw.find('table', attrs = {'class':'no_highlight stats_table'}) 
    for row in table.findAll('tr'): 
     col = row.findAll('td') 
     if col: 
      year = col[0].find(text=True) 
      print year 
0

Çünkü bir tr ve boş gri çizginin olduğunu. col

col = row.findAll('td') 
    if col: 
     year = col[0].find(text=True) 
     print year 

ve

2014-15 
2014-15 
2014-15 
2013-14 
2013-14 
2013-14 
2012-13 
2012-13 
etc 
doğru sonuçları vermesi durumunda bir kontrol yap