爬虫项目总结(四)(pipleline的存取)

数据库的存储:

import pymysql
class mysqlPipeline(object):
    def __init__(self):
        #数据库的连接
        self.connect = pymysql.connections(host="127.0.0.1", user="root", passwd="root", db="test")
        #host   数据库地址   localhost一般
        #user   数据库用户名
        #passwd 数据库密码
        #db     数据库名字
        self.cursor = self.connect.cursor()  #获取连接

    def process_item(self, item, spider):
        sql = "insert into test value('%s','%s')"               #sql语句
        self.cursor.execute(sql, (item['name'], item['sex']))   #执行sql语句
        self.connect.commit()                                   #提交

    def close_spider(self):
        self.cursor.close()
        self.connect.close()

json格式的写入

import json
class jsonPipleline(object):
    def __init__(self):
        self.fb = open('test.json', 'w', encoding='utf-8')#创建写入的文件,设置编码为utf-8
    def process_item(self, item, spider):
        item_json = json.dump(item)  #
        self.fb.write(item_json+'\n')  #进行写入
        return item

    def close_spider(self, spider):
        self.fb.close()  #关闭

csv格式的写入

import csv
class csvPipleline(object):
    def __init__(self):
        self.file = open('test.csv', 'w', newline='')
        self.csvwriter = csv.writer(self.file)
        self.csvwriter.writerow(['名字', '性别'])

    def process_item(self, item, spider):
        self.csvwriter.writerow([item["name"], item["sex"]])
        return item

    def close_spider(self, spider):
        self.file.close()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值