scrapy深度爬蟲(chóng)——02管道模塊與items

管道模塊

# -*- coding: utf-8 -*-

# Define your item pipelines here

#

# Don't forget to add your pipeline to the ITEM_PIPELINES setting

# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

import pymysql

class MyspiderPipeline(object):

def __int__(self):

print('my pipelines is ready....__init__ invoking....')

def open_spider(self,spider):

'''

? ? ? ? 建立數(shù)據(jù)庫(kù)連接

? ? ? ? :paramspider:

? ? ? ? :return:

'''

? ? ? ? self.my_conn = pymysql.connect(

host ="localhost",

port =3306,

database ="jobs",

user ='root',

password ='123',

charset ='utf8'

? ? ? ? )

#獲取游標(biāo)對(duì)象

? ? ? ? self.my_cursor =self.my_conn.cursor()

def process_item(self,item,spider):

print('----------------------process_item is invoking----------------------')

# print item['name']

# print item['salary']

# print item['company']

# print item['day']

# print item['experience']

# print item['area']

# print item['number']

# print item['nature']

# print item['education']

# print item['description']

? ? ? ? insert_sql ="insert into wuxi(name, salary, company, day,experience,area,number,nature,education,description) values(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"

? ? ? ? self.my_cursor.execute(insert_sql,[item['name'],item['salary'],item['company'],item['day'],item['experience'],item['area'],item['number'],item['nature'],item['education'],item['description']])

def close_spider(self, spider):

#提交MySQL語(yǔ)句

? ? ? ? self.my_conn.commit()

#關(guān)閉游標(biāo)對(duì)象與數(shù)據(jù)庫(kù)連接

? ? ? ? self.my_cursor.close()

self.my_conn.close()



items


# -*- coding: utf-8 -*-

# Define here the models for your scraped items

#

# See documentation in:

# https://doc.scrapy.org/en/latest/topics/items.html

import scrapy

class MyspiderItem(scrapy.Item):

# define the fields for your item here like:

# name = scrapy.Field()

? ? name = scrapy.Field()

salary = scrapy.Field()

company = scrapy.Field()

day = scrapy.Field()

experience = scrapy.Field()

area = scrapy.Field()

number = scrapy.Field()

nature = scrapy.Field()

education = scrapy.Field()

description = scrapy.Field()

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容