python中scrapy處理項(xiàng)目數(shù)據(jù)的實(shí)例分析
在我們處理完數(shù)據(jù)后,習(xí)慣把它放在原有的位置,但是這樣也會(huì)出現(xiàn)一定的隱患。如果因?yàn)樾聰?shù)據(jù)的加入或者其他種種原因,當(dāng)我們?cè)俅蜗胍獑⒂眠@個(gè)文件的時(shí)候,小伙伴們就會(huì)開(kāi)始著急卻怎么也翻不出來(lái),似乎也沒(méi)有其他更好的搜集辦法,而重新進(jìn)行數(shù)據(jù)整理顯然是不現(xiàn)實(shí)的。下面我們就一起看看python爬蟲(chóng)中scrapy處理項(xiàng)目數(shù)據(jù)的方法吧。
1、拉取項(xiàng)目
$ git clone https://github.com/jonbakerfish/TweetScraper.git$ cd TweetScraper/$ pip install -r requirements.txt #add ’--user’ if you are not root$ scrapy list$ #If the output is ’TweetScraper’, then you are ready to go.
2、數(shù)據(jù)持久化
通過(guò)閱讀文檔,我們發(fā)現(xiàn)該項(xiàng)目有三種持久化數(shù)據(jù)的方式,第一種是保存在文件中,第二種是保存在Mongo中,第三種是保存在MySQL數(shù)據(jù)庫(kù)中。因?yàn)槲覀冏ト〉臄?shù)據(jù)需要做后期的分析,所以,需要將數(shù)據(jù)保存在MySQL中。
抓取到的數(shù)據(jù)默認(rèn)是以Json格式保存在磁盤(pán) ./Data/tweet/ 中的,所以,需要修改配置文件 TweetScraper/settings.py 。
ITEM_PIPELINES = { # ’TweetScraper.pipelines.SaveToFilePipeline’:100,#’TweetScraper.pipelines.SaveToMongoPipeline’:100, # replace `SaveToFilePipeline` with this to use MongoDB ’TweetScraper.pipelines.SavetoMySQLPipeline’:100, # replace `SaveToFilePipeline` with this to use MySQL}#settings for mysqlMYSQL_SERVER = '18.126.219.16'MYSQL_DB = 'scraper'MYSQL_TABLE = 'tweets' # the table will be created automaticallyMYSQL_USER = 'root' # MySQL user to use (should have INSERT access granted to the Database/TableMYSQL_PWD = 'admin123456' # MySQL user’s password
內(nèi)容擴(kuò)展:
scrapy.cfg是項(xiàng)目的配置文件
from scrapy.spider import BaseSpiderclass DmozSpider(BaseSpider):name = 'dmoz'allowed_domains = ['dmoz.org']start_urls = [ 'http://www.dmoz.org/Computers/Programming/Languages/Python/Books/', 'http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/']def parse(self, response): filename = response.url.split('/')[-2] open(filename, ’wb’).write(response.body)
到此這篇關(guān)于python中scrapy處理項(xiàng)目數(shù)據(jù)的實(shí)例分析的文章就介紹到這了,更多相關(guān)python爬蟲(chóng)中scrapy如何處理項(xiàng)目數(shù)據(jù)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ajax請(qǐng)求添加自定義header參數(shù)代碼2. PHP8.0新功能之Match表達(dá)式的使用3. Android實(shí)現(xiàn)觸發(fā)html頁(yè)面的Button控件點(diǎn)擊事件方式4. JavaScript基于用戶(hù)照片姓名生成海報(bào)5. Nginx+php配置文件及原理解析6. 解決python腳本中error: unrecognized arguments: True錯(cuò)誤7. 解決Python 進(jìn)程池Pool中一些坑8. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究9. 八種Vue組件間通訊方式合集(推薦)10. 無(wú)線(xiàn)標(biāo)記語(yǔ)言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁(yè)
