|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import pymysql
- class XiaoshuospiderPipeline:
- conn = None#mysql的链接对象
- cursor = None
- def open_spider(self,spider):
- #数据库初始
- self.conn = pymysql.Connect(
- host='127.0.0.1',
- port=3306,
- user='root',
- password='123456a',
- db= '爬虫',
- charset='utf8'
- )
- #创建游标
- self.cursor = self.conn.cursor()
- # 爬虫文件每向管道提交一个item,则process_item方法就会被调用一次
- def process_item(self, item, spider):
- title = item['title']
- sql = 'insert into xiaoshuo (title) values ("%s")'%title
- #执行sql语句
- self.cursor.execute(sql)
- #提交事务
- self.conn.commit()
- print('成功写入一条数据')
- return item
- def close_spdier(self,spider):
- self.cursor.close()
- self.conn.close()
复制代码
导入pymysql没有问题,为什么一运行就报错 No module named 'pymysql'
|
|