簡単に言えば、Web サイトから HTML を読み取って、テーブルの値をローカルの MySQL データベースに格納しようとしています。BeautifulSoup4 を使用してテーブルからすべての情報を正常に取得しましたが、MySQL データベースに入れるのに問題があります。
Python 2.7.5 と互換性のある mysql.connector を使用しています。これが私のコードです:
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()
トレースバック [コメントから -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