From d8a983aaebb941162647cfae11eed9aaa30336c4 Mon Sep 17 00:00:00 2001 From: link_1999 <1402246900@qq.com> Date: Wed, 6 Apr 2022 20:16:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20'dagongPro'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增:mysql管道 --- dagongPro/pipelines.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/dagongPro/pipelines.py b/dagongPro/pipelines.py index 096dec2..ef28503 100644 --- a/dagongPro/pipelines.py +++ b/dagongPro/pipelines.py @@ -6,7 +6,7 @@ # useful for handling different item types with a single interface from itemadapter import ItemAdapter - +import pymysql class DagongwangproPipeline: @@ -17,8 +17,30 @@ class DagongwangproPipeline: content = item['content'] date = item['date'] - news_path = './新闻/国际新闻/' + title + '.txt' + 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() \ No newline at end of file