Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python to MySQL db. Errno 9 bad file descriptor

So long story short, I am trying to read an HTML off a website and put the values of the table in a local MySQL database. I successfully pulled all the information off the table using BeautifulSoup4, but I am having trouble with putting it into the MySQL db.

I am using the mysql.connector that is compatible with Python 2.7.5. Here is my code:

import urllib2
from bs4 import BeautifulSoup
import mysql.connector
from mysql.connector import errorcode

# Opens MySQL db and handles all connection errors
dbConfig = {'user':'root',
            'password':'pimovi',
            'host':'127.0.0.1',
            'database':'RateYourMusic'}
try:
    db = mysql.connector.connect(**dbConfig)
    cursor = db.cursor()
except mysql.connector.Error as err:
    if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
        print "Something is wrong with your user name or password"
    elif err.errno == errorcode.ER_BAD_DB_ERROR:
        print "Database does not exist"
    else:
        print err
else:
    db.close()

url = 'http://rateyourmusic.com/customchart'
req = urllib2.Request(url, headers={'User-Agent':'Mozilla/5.0'})
read = urllib2.urlopen(req)
soup = BeautifulSoup(read)

table = soup.find('table', {'class':'mbgen'})

for row in table.findAll('tr'):
    try:
        cells = row.findAll('td')
        rank = int(cells[0].find(class_='ooookiig').text)
        artist = cells[2].find(class_='artist').text
        album = cells[2].find(class_='album').text
        year = cells[2].find(class_='mediumg').text
        year = int(year[1:5])

        entry = {'Rank':rank, 'Artist':artist, 'Album':album, 'Year':year}
        add_album = ("INSERT INTO chartinfo "
                     "(rank_info, artist_info, album_info, year_info) "
                     "VALUES (rank, artist, album, year)")
        cursor.execute(add_album)
        db.commit()
        print entry
    except AttributeError:
        pass

cursor.close()
db.close()

Traceback [from comment -ed]

Traceback (most recent call last): 
File "C:\Programming\RateYourMusicCrawler\AlbumInfoCrawler.py", line 52, in <module> 
    cursor.execute(add_album) 
File "C:\Python27\lib\site-packages\mysql\connector\cursor.py", line 393, in execute
    self._handle_result(self._connection.cmd_query(stmt)) 
File "C:\Python27\lib\site-packages\mysql\connector\connection.py", line 586, in cmd_query 
    statement)) 
File "C:\Python27\lib\site-packages\mysql\connector\connection.py", line 386, in _send_cmd 
    packet_number) 
File "C:\Python27\lib\site-packages\mysql\connector\network.py", line 104, in send_plain
    raise errors.OperationalError(str(err)) 
mysql.connector.errors.OperationalError: [Errno 9] Bad file descriptor
like image 905
The Nomad Avatar asked Mar 21 '23 21:03

The Nomad


1 Answers

This is probably because of:

else:
    db.close()

That closes the connection if your try block doesn't result in any exceptions. Try taking that out.

like image 89
admdrew Avatar answered Apr 02 '23 03:04

admdrew