上传文件至 'dagongPro'

新增:mysql管道
This commit is contained in:
link_1999 2022-04-06 20:16:33 +08:00
parent eace6a5c13
commit d8a983aaeb
1 changed files with 24 additions and 2 deletions

View File

@ -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()