首先第一个问题已经解决了,1054那个报错(CSDN搜的),就是要在values的那个位置里把%s变成%%s
第二个问题搜遍全网都解决不了,front_image_url和front_image_path的错误都是not all arguments converted during bytes formatting,完全不知道是python的常见问题还是mysql的入库问题
class MysqlPipeline(object):
def __init__(self):
self.conn = MySQLdb.connect('127.0.0.1','root','mysql1856','article_spider',charset='utf8',use_unicode=True)
self.cursor = self.conn.cursor()
def process_item(self, item, spider):
insert_sql = """
insert into jobbole_article(title,url,url_object_id,parise_nums,comment_nums,fav_nums,tags,content,create_date)
values(%s,%s,%s,%s,%s,%s,%s,%s,%s)
"""
params = list()
#用get不会抛异常,具体也不太了解,以后测试再说吧
params.append(item.get('title',""))
params.append(item.get('url',""))
params.append(item.get('url_object_id',""))
# front_image = ','.join(item.get('front_image_url',[]))
# params.append(front_image)
# params.append('hello',)
# print(type(item.get('front_image_path',"空的")))
# print(item.get('front_image_path',"空的"))
params.append(item.get('parise_nums',0))
params.append(item.get('comment_nums',0))
params.append(item.get('fav_nums',0))
params.append(item.get('tags',0))
params.append(item.get('content',""))
params.append(item.get('create_date',"1970-07-01")) #给不了空的,只能给个假的,07-01好像是格式,以后再测试一下吧
# # print(params)
self.cursor.execute(insert_sql, tuple(params)) #tuple应该是转换成元组
self.conn.commit() #入库
insert_sql = """
insert into jobbole_article(front_image_path)
values(%%s)
"""
params = list()
params.append('hello', )
self.cursor.execute(insert_sql, tuple(params)) # tuple应该是转换成元组
self.conn.commit() # 入库
return item
以下是老师在视频中的所有数据的类型和我的数据类型的对比,是完全一样啊,该是str就是str,该是int就是int
带你彻底掌握Scrapy,用Django+Elasticsearch搭建搜索引擎
了解课程