Note that there are some explanatory texts on larger screens.

plurals
  1. POPython to MySQL db. Errno 9 bad file descriptor
    primarykey
    data
    text
    <p>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.</p> <p>I am using the mysql.connector that is compatible with Python 2.7.5. Here is my code:</p> <pre><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() </code></pre> <p>Traceback [from comment -ed]</p> <pre><code>Traceback (most recent call last): File "C:\Programming\RateYourMusicCrawler\AlbumInfoCrawler.py", line 52, in &lt;module&gt; 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload