第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

正在回答

3 回答

import?MySQLdb
from?douban.settings?import?mysql_host,?mysql_port,?mysql_user,?mysql_passwd,?mysql_db_name


#?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


class?DoubanPipeline(object):
????def?__init__(self):
????????host?=?mysql_host
????????port?=?mysql_port
????????user?=?mysql_user
????????passwd?=?mysql_passwd
????????dbname?=?mysql_db_name
????????self.mydb?=?MySQLdb.connect(host=host,?port=port,?user=user,?passwd=passwd,?db=dbname,?charset='utf8')
????????self.cursor?=?self.mydb.cursor()

????def?process_item(self,?item,?spider):
????????data?=?dict(item)
????????sql?=?"insert?into?top250?(serial_number,?movie_name,?introduce,?star,?evaluate,?description)?values?(%s,%s,%s,%s,%s,%s)"
????????self.cursor.execute(sql,?(data['serial_number'],?data['movie_name'],?data['introduce'],?data['star'],?data['evaluate'],?data['description']))
????????self.mydb.commit()

????????return?item


0 回復(fù) 有任何疑惑可以回復(fù)我~

?怎么插入到sql server數(shù)據(jù)庫中,mssql?

0 回復(fù) 有任何疑惑可以回復(fù)我~

seetings中

#mysql的基本參數(shù)
sql_host?=?'localhost'
sql_db_name?=?'spider'
sql_sheetname?=?'douban'
sql_user='root'
sql_password=''

pipelines中

#構(gòu)造函數(shù)用于初始化mysql的各項(xiàng)參數(shù)
def?__init__(self):
????host?=?sql_host
????dbname?=sql_db_name
????ruser?=?sql_user
????rpassword?=?sql_password
????self.sheetname?=?sql_sheetname
????#連接數(shù)據(jù)庫
????self.conn?=?pymysql.connect(host=host,?user=ruser,?password=rpassword,?db=dbname,?charset='utf8')
????#?創(chuàng)建一個(gè)游標(biāo)
????self.cursor?=?self.conn.cursor()
#防止連接出現(xiàn)錯(cuò)誤
def?open_spider(self,spider):
????try:
????????host?=?sql_host
????????dbname?=?sql_db_name
????????ruser?=?sql_user
????????rpassword?=?sql_password
????????self.sheetname?=?sql_sheetname
????????#?連接數(shù)據(jù)庫
????????self.conn?=?pymysql.connect(host=host,?user=ruser,?password=rpassword,?db=dbname,?charset='utf8')
????????#?創(chuàng)建一個(gè)游標(biāo)
????????self.cursor?=?self.conn.cursor()
????except:
????????self.open_spider()
????else:
????????spider.logger.info('MySQL:?connected')
????????self.cursor?=?self.conn.cursor(pymysql.cursors.DictCursor)
????????spider.cursor?=?self.cursor

def?process_item(self,?item,?spider):
????#item是從douban_spider里傳出的數(shù)據(jù)
????#先將數(shù)據(jù)轉(zhuǎn)換為字典形式
????data?=?dict(item)
????#mysql插入數(shù)據(jù)
????keys?=?','.join(data.keys())
????values?=?','.join(['%s']?*?len(data))
????sql?=?'INSERT?INTO?{table}({keys})?VALUES({values})'.format(table=self.sheetname,?keys=keys,?values=values)
????try:
????????self.cursor.execute(sql,tuple(data.values()))
????????self.conn.commit()
????except:
????????self.conn.rollback()
????#self.cursor.close()
????#self.conn.close()
????#mongodb插入數(shù)據(jù)
????#self.post.insert(data)
????return?item
def?close_spider(self,spider):
????self.cursor.close()
????self.conn.close()

注意 存儲(chǔ)中字段不能叫describe,好像是python中的關(guān)鍵字,改為describes或其他

0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

botao555

你好,請(qǐng)問你的close_spider函數(shù)在哪里調(diào)用的呢?
2020-09-23 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

怎么用mysql保存數(shù)據(jù)

我要回答 關(guān)注問題
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)