摘要:百度云搜索,搜各種資料搜網(wǎng)盤,搜各種資料注意數(shù)據(jù)保存的操作都是在文件里操作的將數(shù)據(jù)保存為文件是一個信號檢測導入圖片下載器模塊定義數(shù)據(jù)處理類,必須繼承初始化時打開文件為數(shù)據(jù)處理函數(shù),接收一個,里就是爬蟲最后來的數(shù)據(jù)對象文章標題是
【百度云搜索,搜各種資料:http://www.bdyss.cn】 【搜網(wǎng)盤,搜各種資料:http://www.swpan.cn】
注意:數(shù)據(jù)保存的操作都是在pipelines.py文件里操作的
將數(shù)據(jù)保存為json文件
spider是一個信號檢測
#?-*-?coding:?utf-8?-*- #?Define?your?item?pipelines?here # #?Don"t?forget?to?add?your?pipeline?to?the?ITEM_PIPELINES?setting #?See:?http://doc.scrapy.org/en/latest/topics/item-pipeline.html from?scrapy.pipelines.images?import?ImagesPipeline??#導入圖片下載器模塊 import?codecs import?json class?AdcPipeline(object):??????????????????????#定義數(shù)據(jù)處理類,必須繼承object ????def?__init__(self): ????????self.file?=?codecs.open("shuju.json",?"w",?encoding="utf-8")??#初始化時打開json文件 ????def?process_item(self,?item,?spider):???????#process_item(item)為數(shù)據(jù)處理函數(shù),接收一個item,item里就是爬蟲最后yield?item?來的數(shù)據(jù)對象 ????????#?print("文章標題是:"?+?item["title"][0]) ????????#?print("文章縮略圖url是:"?+?item["img"][0]) ????????#?print("文章縮略圖保存路徑是:"?+?item["img_tplj"])??#接收圖片下載器填充的,圖片下載后的路徑 ????????#將數(shù)據(jù)保存為json文件 ????????lines?=?json.dumps(dict(item),?ensure_ascii=False)?+?" "???#將數(shù)據(jù)對象轉(zhuǎn)換成json格式 ????????self.file.write(lines)??????????#將json格式數(shù)據(jù)寫入文件 ????????return?item def?spider_closed(self,spider):?????#創(chuàng)建一個方法繼承spider,spider是一個信號,當前數(shù)據(jù)操作完成后觸發(fā)這個方法 ????????self.file.close()???????????????#關(guān)閉打開文件 class?imgPipeline(ImagesPipeline):??????????????????????#自定義一個圖片下載內(nèi),繼承crapy內(nèi)置的ImagesPipeline圖片下載器類 ????def?item_completed(self,?results,?item,?info):??????#使用ImagesPipeline類里的item_completed()方法獲取到圖片下載后的保存路徑 ????????for?ok,?value?in?results: ????????????img_lj?=?value["path"]?????#接收圖片保存路徑 ????????????#?print(ok) ????????????item["img_tplj"]?=?img_lj??#將圖片保存路徑填充到items.py里的字段里 ????????return?item????????????????????#將item給items.py?文件的容器函數(shù) ????#注意:自定義圖片下載器設(shè)置好后,需要在
將數(shù)據(jù)保存到數(shù)據(jù)庫
我們使用一個ORM框架sqlalchemy模塊,保存數(shù)據(jù)
數(shù)據(jù)庫操作文件
#!/usr/bin/env?python #?-*-?coding:utf-8?-*- from?sqlalchemy.ext.declarative?import?declarative_base from?sqlalchemy?import?Column from?sqlalchemy?import?Integer,?String,?TIMESTAMP from?sqlalchemy?import?ForeignKey,?UniqueConstraint,?Index from?sqlalchemy.orm?import?sessionmaker,?relationship from?sqlalchemy?import?create_engine #配置數(shù)據(jù)庫引擎信息 ENGINE?=?create_engine("mysql+pymysql://root:[email protected]:3306/cshi?charset=utf8",?max_overflow=10,?echo=True) Base?=?declarative_base()???????#創(chuàng)建一個SQLORM基類 class?SendMsg(Base):????????????#設(shè)計表 ????__tablename__?=?"sendmsg" ????id?=?Column(Integer,?primary_key=True,?autoincrement=True) ????title?=?Column(String(300)) ????img_tplj?=?Column(String(300)) def?init_db(): ????Base.metadata.create_all(ENGINE)????????#向數(shù)據(jù)庫創(chuàng)建指定表 def?drop_db(): ????Base.metadata.drop_all(ENGINE)??????????#向數(shù)據(jù)庫刪除指定表 def?session(): ????cls?=?sessionmaker(bind=ENGINE)?????????#創(chuàng)建sessionmaker類,操作表 ????return?cls() #?drop_db()?????????#刪除表 #?init_db()?????????#創(chuàng)建表
pipelines.py文件
#?-*-?coding:?utf-8?-*- #?Define?your?item?pipelines?here # #?Don"t?forget?to?add?your?pipeline?to?the?ITEM_PIPELINES?setting #?See:?http://doc.scrapy.org/en/latest/topics/item-pipeline.html from?scrapy.pipelines.images?import?ImagesPipeline??#導入圖片下載器模塊 from?adc?import?shujuku?as?ORM??????????????????????#導入數(shù)據(jù)庫文件 class?AdcPipeline(object):??????????????????????#定義數(shù)據(jù)處理類,必須繼承object ????def?__init__(self): ????????ORM.init_db()???????????????????????????#創(chuàng)建數(shù)據(jù)庫表 ????def?process_item(self,?item,?spider):???????#process_item(item)為數(shù)據(jù)處理函數(shù),接收一個item,item里就是爬蟲最后yield?item?來的數(shù)據(jù)對象 ????????print("文章標題是:"?+?item["title"][0]) ????????print("文章縮略圖url是:"?+?item["img"][0]) ????????print("文章縮略圖保存路徑是:"?+?item["img_tplj"])??#接收圖片下載器填充的,圖片下載后的路徑 ????????mysq?=?ORM.session() ????????shuju?=?ORM.SendMsg(title=item["title"][0],?img_tplj=item["img_tplj"]) ????????mysq.add(shuju) ????????mysq.commit() ????????return?item class?imgPipeline(ImagesPipeline):??????????????????????#自定義一個圖片下載內(nèi),繼承crapy內(nèi)置的ImagesPipeline圖片下載器類 ????def?item_completed(self,?results,?item,?info):??????#使用ImagesPipeline類里的item_completed()方法獲取到圖片下載后的保存路徑 ????????for?ok,?value?in?results: ????????????img_lj?=?value["path"]?????#接收圖片保存路徑 ????????????#?print(ok) ????????????item["img_tplj"]?=?img_lj??#將圖片保存路徑填充到items.py里的字段里 ????????return?item????????????????????#將item給items.py?文件的容器函數(shù) ????#注意:自定義圖片下載器設(shè)置好后,需要在
【轉(zhuǎn)載自:http://www.lqkweb.com】
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/45126.html
摘要:百度云搜索,搜各種資料搜網(wǎng)盤,搜各種資料編寫爬蟲文件循環(huán)抓取內(nèi)容方法,將指定的地址添加到下載器下載頁面,兩個必須參數(shù),參數(shù)頁面處理函數(shù)使用時需要方法,是庫下的方法,是自動拼接,如果第二個參數(shù)的地址是相對路徑會自動與第一個參數(shù)拼接導 【百度云搜索,搜各種資料:http://bdy.lqkweb.com】 【搜網(wǎng)盤,搜各種資料:http://www.swpan.cn】 編寫spiders爬...
摘要:百度云搜索,搜各種資料搜網(wǎng)盤,搜各種資料我們自定義一個來作為啟動文件導入執(zhí)行命令方法給解釋器,添加模塊新路徑將文件所在目錄添加到解釋器執(zhí)行命令爬蟲文件表達式基本使用設(shè)置爬蟲起始域名設(shè)置爬蟲起始地址默認爬蟲回調(diào)函數(shù),返 【百度云搜索,搜各種資料:http://www.bdyss.cn】 【搜網(wǎng)盤,搜各種資料:http://www.swpan.cn】 我們自定義一個main.py來作為啟動...
摘要:百度云搜索,搜各種資料搜網(wǎng)盤,搜各種資料用命令創(chuàng)建自動爬蟲文件創(chuàng)建爬蟲文件是根據(jù)的母版來創(chuàng)建爬蟲文件的查看創(chuàng)建爬蟲文件可用的母版母版說明創(chuàng)建基礎(chǔ)爬蟲文件創(chuàng)建自動爬蟲文件創(chuàng)建爬取數(shù)據(jù)爬蟲文件創(chuàng)建爬取數(shù)據(jù)爬蟲文件創(chuàng)建一個基礎(chǔ)母版爬蟲,其他同理 【百度云搜索,搜各種資料:http://www.bdyss.cn】 【搜網(wǎng)盤,搜各種資料:http://www.swpan.cn】 用命令創(chuàng)建自動爬...
摘要:百度云搜索,搜各種資料搜網(wǎng)盤,搜各種資料基本概念反爬蟲的目的爬蟲和反爬的對抗過程以及策略架構(gòu)源碼分析圖 【百度云搜索,搜各種資料:http://www.lqkweb.com】 【搜網(wǎng)盤,搜各種資料:http://www.swpan.cn】 1、基本概念 showImg(https://segmentfault.com/img/remote/1460000019749170); 2、反爬...
摘要:百度云搜索,搜各種資料搜網(wǎng)盤,搜各種資料請求請求就是我們在爬蟲文件寫的方法,也就是提交一個請求地址,請求是我們自定義的方法提交一個請求參數(shù)字符串類型地址回調(diào)函數(shù)名稱字符串類型請求方式,如果字典類型的,瀏覽器用戶代理設(shè)置字典類型鍵值對,向回調(diào) 【百度云搜索,搜各種資料:http://www.lqkweb.com】 【搜網(wǎng)盤,搜各種資料:http://www.swpan.cn】 Reque...
閱讀 1980·2021-11-24 10:45
閱讀 1468·2021-11-18 13:15
閱讀 4562·2021-09-22 15:47
閱讀 3941·2021-09-09 11:36
閱讀 2018·2019-08-30 15:44
閱讀 3097·2019-08-29 13:05
閱讀 2510·2019-08-29 12:54
閱讀 2003·2019-08-26 13:47