0

I am new in Python and try to scrape data from the web to (eventually) feed a small database.

My code is generating a NoneType error. Could you assist?

import urllib2
from bs4 import BeautifulSoup
    #1- create files to Leagues, stock data and error
FLeague= open("C:\Python\+exercice\SoccerLeague.txt","w")
FData=open("C:\Python\+exercice\FootballDump.txt","w")
ErrorFile=open("C:\Python\+exercice\ErrorFootballScrap.txt","w")
#Open the website
# 1- grab the data and get the error too
soup = BeautifulSoup(urllib2.urlopen("http://www.soccerstats.com/leagues.asp").read(),"html")
TableLeague =  soup.find("table", {"class" : "trow8"})
print TableLeague
#\here I just want to grab country name
for row in TableLeague("tr")[2:]:
       col = row.findAll("td")
# I try to identify errors
       try:
          country = col[1].a.string.stip()
          FLeague.write(country+"\n")
       except Exception as e:
          ErrorFile.write (country + ";" + str(e)+";"+str(col)+"\n")
          pass
    #close the files
FLeague.close
FData.close
ErrorFile.close
zondo
  • 19,901
  • 8
  • 44
  • 83
lucTiber
  • 411
  • 1
  • 4
  • 18
  • 1
    What is `stip()` in `col[1].a.string.stip()`? – vaultah Mar 04 '16 at 18:22
  • @vaultah: It's probably a typo. He probably means `strip()`. – zondo Mar 04 '16 at 18:25
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your code and accurately describe the problem. This includes code that reproduces the problem, and the full error message. – Prune Mar 04 '16 at 18:29

1 Answers1

0

The first problem is coming from:

TableLeague("tr")[2:]

TableLeague is None here since there is no table element with trow8 class. Instead use the id attribute to find the desired table element:

TableLeague = soup.find("table", {"id": "btable"})

Also, you probably meant strip() and not stip() here: col[1].a.string.stip().

And, in order to close the files, call the close() method. Replace:

FLeague.close
FData.close
ErrorFile.close

with:

FLeague.close()
FData.close()
ErrorFile.close()

Or, even better, use with context manager to work with files - you would not need to close a file explicitly.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195