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. 如何通過(guò)eclipse web項(xiàng)目導(dǎo)入itellij idea并啟動(dòng)2. jsp實(shí)現(xiàn)登錄界面3. PHP未來(lái)的一些可能4. IDEA 端口占用的解決方法(推薦)5. 刪除docker里建立容器的操作方法6. 關(guān)于在IDEA中SpringBoot項(xiàng)目中activiti工作流的使用詳解7. 使用vue寫(xiě)一個(gè)翻頁(yè)的時(shí)間插件實(shí)例代碼8. python 服務(wù)器運(yùn)行代碼報(bào)錯(cuò)ModuleNotFoundError的解決辦法9. 使用IDEA編寫(xiě)jsp時(shí)EL表達(dá)式不起作用的問(wèn)題及解決方法10. 將Git存儲(chǔ)庫(kù)克隆到本地IntelliJ IDEA項(xiàng)目中的詳細(xì)教程
