2013-04-12 120 views
0

我想從使用嵌套循環的此網站獲取所有表格。我幾乎在那裏,但仍然不確定幾個具有相同類標識符的表的循環。我得到一個錯誤代碼爲line 26 : for s in soup.findALL ("table", { "class" : "boxScore"})使用嵌套循環從beautifulsoup獲取所有表格使用嵌套循環

SyntaxError: invalid syntax.

我的腳本:

import datetime 
import urllib 
from bs4 import BeautifulSoup 
import urllib2 


day = int(datetime.datetime.now().strftime("%d"))-1 

month = datetime.datetime.now().strftime("%B") 
year = datetime.datetime.now().strftime("%Y") 
file_name = "https://stackoverflow.com/users/ripple/NHL.csv" 
file = open(file_name,"w") 
url = "http://www.tsn.ca/nhl/scores/?date=" + month + "/" + str(day) + "/" + year 
print 'Grabbing from: ' + url + '...\n' 
try: 
     r = urllib2.urlopen(url) 
except urllib2.URLError as e: 
      r = e 
if r.code in (200, 401):  
    #get the table data from the page 
    data = urllib.urlopen(url).read() 
    #send to beautiful soup 
    soup = BeautifulSoup(data) 
    print soup 
    soup = soup.findALL ("table", { "class" : "boxScore"}) 
    for s in soup.findALL ("table", { "class" : "boxScore"}) 
     table = soup.find("table",{ "class" : "boxScore"}) 
     for tr in table.findAll('tr')[2:]: 
      col = tr.findAll('td') 
      team = col[0].get_text().encode('ascii','ignore').replace(" ","") 
      firstp = col[1].get_text().encode('ascii','ignore').replace(" ","") 
      secondp = col[2].get_text().encode('ascii','ignore').replace(" ","") 
      thirdp = col[3].get_text().encode('ascii','ignore').replace(" ","") 
      final = col[4].get_text().encode('ascii','ignore').replace(" ","") 
      record = team + ',' + final + '\n' 
      print record 
      file.write(record) 
else: 
    print str(i) + " NO GAMES" 
file.close() 

回答

2

在Python中循環使用冒號結束 ':'。另外:API方法是findAll()而不是findALL()。

+0

哇,我很感激!謝謝! –