課程
/后端開發(fā)
/Python
/Python最火爬蟲框架Scrapy入門與實(shí)踐
如題,求詳細(xì)代碼
2019-05-23
源自:Python最火爬蟲框架Scrapy入門與實(shí)踐 2-8
正在回答
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
?怎么插入到sql server數(shù)據(jù)庫中,mssql?
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或其他
botao555
舉報(bào)
做為爬蟲工程師Python Scrapy主流爬蟲框架你必須要會(huì)!
2 回答使用MySql如何插入數(shù)據(jù)呢
1 回答如何用 :w保存修改的settings.py
1 回答我在Windows上操作的保存CSV文件怎么操作呀
1 回答如何存儲(chǔ)到mysql
1 回答我保存到mysql里,想問下在哪里執(zhí)行close連接的操作呢?process_item函數(shù)里關(guān)閉不行吧?關(guān)掉之后下條數(shù)據(jù)就插不進(jìn)去了
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2019-11-26
2019-09-11
?怎么插入到sql server數(shù)據(jù)庫中,mssql?
2019-08-07
seetings中
pipelines中
注意 存儲(chǔ)中字段不能叫describe,好像是python中的關(guān)鍵字,改為describes或其他