首先要做的事情。我在Python 2.7.2+上运行我的Scrapy脚本(版本:0.16.2)。我遇到的问题是使用MySQLdb库生成的管道文件和SQL语法。 这是我试图运行的代码:
class basketpipeline(object):
def __init__(self):
self.conn = MySQLdb.connect(user="blabla", passwd="blabla", db="basketball", host="blabla")
self.cursor = self.conn.cursor()
def process_item(self,item, spider):
#declare variables for item elements
Game_id=item['game_id']
Attendance=item['attendance']
Referees=item['referees']
#form sql for database implementing variables above into the SQL
self.cursor.execute("""INSERT INTO BasketGame (Game_id,Referees,Attendance) VALUES (%s,%s,%s)""", (Game_id,Referees,Attenance))
self.conn.commit()
return item
在我运行我的脚本后,我在shell中有这个:
2013-11-14 00:32:59+0200 [scrapy] INFO: Scrapy 0.16.2 started (bot: basketbase)
2013-11-14 00:32:59+0200 [scrapy] DEBUG: Enabled extensions: LogStats, TelnetConsole,
CloseSpider, WebService, CoreStats, SpiderState
2013-11-14 00:32:59+0200 [scrapy] DEBUG: Enabled downloader middlewares: HttpAuthMiddleware, DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware, DefaultHeadersMiddleware, RedirectMiddleware, CookiesMiddleware, HttpCompressionMiddleware, ChunkedTransferMiddleware, DownloaderStats
2013-11-14 00:32:59+0200 [scrapy] DEBUG: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddleware
2013-11-14 00:32:59+0200 [scrapy] DEBUG: Enabled item pipelines: basketpipeline
2013-11-14 00:32:59+0200 [basketsp] INFO: Spider opened
2013-11-14 00:32:59+0200 [basketsp] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2013-11-14 00:32:59+0200 [scrapy] DEBUG: Telnet console listening on 0.0.0.0:6023
2013-11-14 00:32:59+0200 [scrapy] DEBUG: Web service listening on 0.0.0.0:6080
2013-11-14 00:33:01+0200 [basketsp] DEBUG: Crawled (200) <GET http://www.euroleague.net/main/results/showgame?gamecode=46&s%09code=E2013> (referer: None)
2013-11-14 00:33:01+0200 [basketsp] ERROR: Error processing %(item)s
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/Scrapy-0.16.2-py2.7.egg/scrapy/middleware.py", line 62, in _process_chain
return process_chain(self.methods[methodname], obj, *args)
File "/usr/local/lib/python2.7/dist-packages/Scrapy-0.16.2-py2.7.egg/scrapy/utils/defer.py", line 65, in process_chain
d.callback(input)
File "/usr/local/lib/python2.7/dist-packages/Twisted-12.2.0-py2.7-linux-i686.egg/twisted/internet/defer.py", line 368, in callback
self._startRunCallbacks(result)
File "/usr/local/lib/python2.7/dist-packages/Twisted-12.2.0-py2.7-linux-i686.egg/twisted/internet/defer.py", line 464, in _startRunCallbacks
self._runCallbacks()
--- <exception caught here> ---
File "/usr/local/lib/python2.7/dist-packages/Twisted-12.2.0-py2.7-linux-i686.egg/twisted/internet/defer.py", line 551, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "/home/vy/basketbase/basketbase/pipelines.py", line 41, in process_item
self.cursor.execute("""INSERT INTO BasketGame (Game_id,Referees,Attendance) VALUES (%s,%s,%s)""", (Game_id,Referees,Attendance))
File "/usr/lib/pymodules/python2.7/MySQLdb/cursors.py", line 174, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/pymodules/python2.7/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.ProgrammingError: (1064, 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'),("\'BULTO, VICENTE (ESP), SAHIN, TOLGA (ITA), VYKLICKY, ROBERT (CZE)\'",),("\'695\' at line 1')
错误:
`_mysql_exceptions.ProgrammingError: (1064, 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'),("\'BULTO, VICENTE (ESP), SAHIN, TOLGA (ITA), VYKLICKY, ROBERT (CZE)\'",),("\'695\' at line 1`'
如果我只使用变量(这三个中的任何一个)就可以了,它运行正常,但是当我开始使用两个或更多时,我得到了同样的问题。 我试着解决这个问题,阅读文档: http://mysql-python.sourceforge.net/MySQLdb.html
在Stackoverflow中找到了很好的答案: Python MySQL Parameterized Queries
但是如果我使用多个变量,我会收到错误。 我知道我可能会错过这里的一些东西,因为我是Python / Scrapy / SQL的新手,但如果有人可以提供帮助,那就太棒了。提前致谢。