From ed11bcace2af085e565809685957011c81256c5a Mon Sep 17 00:00:00 2001 From: link_1999 <1402246900@qq.com> Date: Mon, 11 Apr 2022 10:30:02 +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 # 4.11新增:新闻板块代码index 在首页获取到新闻板块的代码index并传递到管道中,在管道中基于Index判断该新闻属于哪个板块,并存入mysql的对应新闻表中。 --- dagongPro/items.py | 2 ++ dagongPro/pipelines.py | 27 +++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/dagongPro/items.py b/dagongPro/items.py index 5f806e9..41d9d02 100644 --- a/dagongPro/items.py +++ b/dagongPro/items.py @@ -13,3 +13,5 @@ class DagongwangproItem(scrapy.Item): src = scrapy.Field() author = scrapy.Field() news_id = scrapy.Field() + # 4.11新增:新闻板块代码index + index = scrapy.Field() diff --git a/dagongPro/pipelines.py b/dagongPro/pipelines.py index d12bf93..e314fc9 100644 --- a/dagongPro/pipelines.py +++ b/dagongPro/pipelines.py @@ -37,9 +37,32 @@ class mysqlPileLine(object): def process_item(self, item, spider): self.cursor = self.conn.cursor() + # 4.11新增:新闻板块代码index + # print(item['index']) # 成功打印说明可以将index的值从dagong.py传递到此 + index = item['index'] + try: - self.cursor.execute('insert into tw_news values("%s", "%s", "%s", "%s", "%s", "%s")'%(item['news_id'], item['title'], item['date'], item['author'], item['content'], item['src'])) - self.conn.commit() + # 4.11新增:新闻板块代码index,此处可依据index代码将新闻分类保存至mysql数据库的不同新闻表中 + if index == 0: + self.cursor.execute('insert into nd_news values("%s", "%s", "%s", "%s", "%s", "%s")' % ( + item['news_id'], item['title'], item['date'], item['author'], item['content'], item['src'])) + self.conn.commit() + elif index == 2: + self.cursor.execute('insert into xg_news values("%s", "%s", "%s", "%s", "%s", "%s")' % ( + item['news_id'], item['title'], item['date'], item['author'], item['content'], item['src'])) + self.conn.commit() + elif index == 4: + self.cursor.execute('insert into tw_news values("%s", "%s", "%s", "%s", "%s", "%s")' % ( + item['news_id'], item['title'], item['date'], item['author'], item['content'], item['src'])) + self.conn.commit() + elif index == 6: + self.cursor.execute('insert into gj_news values("%s", "%s", "%s", "%s", "%s", "%s")' % ( + item['news_id'], item['title'], item['date'], item['author'], item['content'], item['src'])) + self.conn.commit() + elif index == 8: + self.cursor.execute('insert into js_news values("%s", "%s", "%s", "%s", "%s", "%s")' % ( + item['news_id'], item['title'], item['date'], item['author'], item['content'], item['src'])) + self.conn.commit() except Exception as e: print(e) self.conn.rollback()