文章目录
步骤1:构造Item
import scrapy
class StockItem(scrapy.Item):
stock_code = scrapy.Field()
company_name = scrapy.Field()
stock_type = scrapy.Field()
class CompanyInfoItem(scrapy.Item):
name = scrapy.Field()
company_name = scrapy.Field()
步骤2:构造Pipeline
from .items import StockItem, CompanyInfoItem
import pymysql
class MyPipeline(object):
def __init__(self): # 这里可以定义数据库的一些配置
host = "127.0.0.1"
user = "testuser"
password = "testpassword"
db = "test_db"
self.conn = pymysql.connect(host=host, user=user, password=password, database=db)
self.cursor = self.conn.cursor()
def process_item(self, item, spider):
if isinstance(item, StockItem):
print("StockItem") # StockItem的处理逻辑
elif isinstance(item, CompanyInfoItem):
print('CompanyInfoItem') # CompanyInfoItem的处理逻辑
步骤3:setting配置pipeline
ITEM_PIPELINES = {
'company_finance.pipelines.MyPipeline': 300,
}