python中scrapy處理項目數(shù)據(jù)的實例分析
在我們處理完數(shù)據(jù)后,習慣把它放在原有的位置,但是這樣也會出現(xiàn)一定的隱患。如果因為新數(shù)據(jù)的加入或者其他種種原因,當我們再次想要啟用這個文件的時候,小伙伴們就會開始著急卻怎么也翻不出來,似乎也沒有其他更好的搜集辦法,而重新進行數(shù)據(jù)整理顯然是不現(xiàn)實的。下面我們就一起看看python爬蟲中scrapy處理項目數(shù)據(jù)的方法吧。
1、拉取項目
$ 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ù)持久化
通過閱讀文檔,我們發(fā)現(xiàn)該項目有三種持久化數(shù)據(jù)的方式,第一種是保存在文件中,第二種是保存在Mongo中,第三種是保存在MySQL數(shù)據(jù)庫中。因為我們抓取的數(shù)據(jù)需要做后期的分析,所以,需要將數(shù)據(jù)保存在MySQL中。
抓取到的數(shù)據(jù)默認是以Json格式保存在磁盤 ./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 mysql MYSQL_SERVER = "18.126.219.16" MYSQL_DB = "scraper" MYSQL_TABLE = "tweets" # the table will be created automatically MYSQL_USER = "root" # MySQL user to use (should have INSERT access granted to the Database/Table MYSQL_PWD = "admin123456" # MySQL user's password
內容擴展:
scrapy.cfg是項目的配置文件
from scrapy.spider import BaseSpider class 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)
到此這篇關于python中scrapy處理項目數(shù)據(jù)的實例分析的文章就介紹到這了,更多相關python爬蟲中scrapy如何處理項目數(shù)據(jù)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python判斷素數(shù)的3種方法及for-else語句的用法介紹
素數(shù)又叫質數(shù),指的是>1的整數(shù)中,只能被1和這個數(shù)本身整除的數(shù),這篇文章主要給大家介紹了關于Python判斷素數(shù)的3種方法及for-else語句的用法介紹的相關資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-05-05用python簡單實現(xiàn)mysql數(shù)據(jù)同步到ElasticSearch的教程
今天小編就為大家分享一篇用python簡單實現(xiàn)mysql數(shù)據(jù)同步到ElasticSearch的教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05ubuntu安裝sublime3并配置python3環(huán)境的方法
這篇文章主要介紹了ubuntu安裝sublime3并配置python3環(huán)境的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03