python中scrapy處理項目數(shù)據(jù)的實例分析
在我們處理完數(shù)據(jù)后,習(xí)慣把它放在原有的位置,但是這樣也會出現(xiàn)一定的隱患。如果因為新數(shù)據(jù)的加入或者其他種種原因,當(dāng)我們再次想要啟用這個文件的時候,小伙伴們就會開始著急卻怎么也翻不出來,似乎也沒有其他更好的搜集辦法,而重新進行數(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ù)默認(rèn)是以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 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)容擴展:
scrapy.cfg是項目的配置文件
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處理項目數(shù)據(jù)的實例分析的文章就介紹到這了,更多相關(guān)python爬蟲中scrapy如何處理項目數(shù)據(jù)內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Hybris在idea中debug配置方法詳解2. 在idea中為注釋標(biāo)記作者日期操作3. jsp cookie+session實現(xiàn)簡易自動登錄4. XPath入門 - XSL教程 - 35. 通過CSS數(shù)學(xué)函數(shù)實現(xiàn)動畫特效6. .NET Core Web APi類庫內(nèi)嵌運行的方法7. .NET6使用ImageSharp實現(xiàn)給圖片添加水印8. ASP.NET MVC實現(xiàn)橫向展示購物車9. ASP.NET MVC使用Boostrap實現(xiàn)產(chǎn)品展示、查詢、排序、分頁10. .net如何優(yōu)雅的使用EFCore實例詳解
