1.使用mysqldb库1.2.5版本。在scrapy的pipeline文件中进行插入数据库操作时,报错。
2.这是报错的完整信息
这是对应的pipeline文件
import MySQLdb
import os.path
class ProjectPipeline(object):
def process_item(self, item, spider):
name = item['name'].encode('utf8')
location = item['location'].encode('utf8')
describe = item['describe'].encode('utf8')
picture = item['picture'].encode('utf8')
comment = item['comment'].encode('utf8')
conn = MySQLdb.connect(
host = 'localhost',
port = 3306,
user = 'root',
passwd = '3285109',
db = 'django',
charset = 'utf8')
cur = conn.cursor()
cur.execute("INSERT INTO django_tmp(name,location,describe,picture,comment) values(%s,%s,%s,%s,%s)", (name,location,describe,picture,comment))
cur.close()
conn.commit()
return item
再送上我的数据库的表
3.最可气的就是我在其他的scrapy项目中成功的对数据库成功地进行了插入!!!
这是代码
import MySQLdb
import os.path
class WeatherPipeline(object):
def process_item(self, item, spider):
cityDate = item['cityDate'].encode('utf8')
week = item['week'].encode('utf8')
img = os.path.basename(item['img'])
temperature = item['temperature'].encode('utf8')
weather = item['weather'].encode('utf8')
wind = item['wind'].encode('utf8')
conn = MySQLdb.connect(
host = 'localhost',
port = 3306,
user = 'root',
passwd = '3285109',
db = 'scrapyDB',
charset = 'utf8')
cur = conn.cursor()
cur.execute("INSERT INTO weather(cityDate,week,img,temperature,weather,wind) values(%s,%s,%s,%s,%s,%s)", (cityDate,week,img,temperature,weather,wind))
cur.close()
conn.commit()
conn.close()
return item
这是表的设计
再来就是运行成功的样子
再到数据库中查看一下!果然是有数据的
4.那么问题来了!!!为什么我检查了很多遍也没有发现到底两者之间有什么不同,导致我的第一个文件语法错误!!!我是新手,在做毕业设计,真心求大神不要嫌弃,给我指点一下!谢谢!
。。。。。时隔这么长的时间再上来看这个问题!!我搞懂了!!!
python所谓的格式字符串!!!
a = 'ztm'
print 'ab%scd'%a
则会输出abztmcd
a = 'ztm'
b = 123
print 'ab%scd%de'%(a,b)
则会输出abztmcd123de