Dyl*_*l10 1 python mysql scrapy
这是我的 pipelines.py,我在第 18 行收到一个错误。
import sys;sys.path.append("/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages")
import MySQLdb
import hashlib
from scrapy.exceptions import DropItem
from scrapy.http import Request
class TestPipeline(object):
def __init__(self):
self.conn = MySQLdb.connect(user='test', passwd='password', db='c1024403', host='ephesus.cs.cf.ac.uk', charset='utf8', use_unicode=True)
self.cursor = self.conn.cursor()
def process_item(self, item, spider):
try:
self.cursor.execute("""INSERT INTO test (test1, test2) VALUES (%s, %s)""", (item['Country'], item['Qualification']))
self.conn.commit()
except MySQLdb.Error, e:
print 'Error %d: %s' % (e.args[0], e.args[1])
sys.exit(1)
return item
Run Code Online (Sandbox Code Playgroud)
这是错误 -
File "project\pipelines.py", line 18
except MySQLdb.Error, e:
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
我已经安装了 mysql-python 和 Visual c++ 2008 express,我不明白错误意味着什么,因为我在互联网上的其他任何地方都找不到关于它的任何信息。
您正在使用 运行您的代码Python 3.x,但您的try.. except部分代码方案是为Python 2.X.
如果要使用 运行代码Python 3.x,请更改此行:
except MySQLdb.Error, e:
Run Code Online (Sandbox Code Playgroud)
到:
except MySQLdb.Error as e:
Run Code Online (Sandbox Code Playgroud)
如果您希望这部分代码Python 2.x也与和一起使用,Python 3.x请将其更改为:
except MySQLdb.Error:
e = sys.exc_info()[1]
Run Code Online (Sandbox Code Playgroud)
阅读更多。
但是根据您的print声明,您为 编写脚本Python 2.x,因此最好使用Python 2.x, 而不是Python 3.x
这sys.path.append("../python2.7/site-packages")行在脚本的第一行也很奇怪。
此外,您粘贴的第一个代码的缩进是错误的,我认为您仍在使用它,请使用您的问题中的当前编辑版本。