crawler-MiniProgram/dagongPro/pipelines.py

46 lines
1.3 KiB
Python

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import pymysql
class DagongwangproPipeline:
def process_item(self, item, spider):
# print(item)
title = item['title']
content = item['content']
date = item['date']
news_path = './新闻/军事新闻/' + title + '.txt'
with open(news_path, 'w', encoding='utf-8') as fp:
fp.write(date+'\n'+content)
return item
class mysqlPileLine(object):
conn = None
cursor = None
def open_spider(self, spider):
self.conn = pymysql.Connect(host='127.0.0.1', port=3306, user='root', password='woaini', db='dagong', charset='utf8')
def process_item(self, item, spider):
self.cursor = self.conn.cursor()
try:
self.cursor.execute('insert into junshi_news values("%s", "%s", "%s")'%(item['title'], item['date'], item['content']))
self.conn.commit()
except Exception as e:
print(e)
self.conn.rollback()
return item
def close_spider(self, spider):
self.cursor.close()
self.conn.close()