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 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
內容擴展:
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)
到此這篇關于python中scrapy處理項目數(shù)據(jù)的實例分析的文章就介紹到這了,更多相關python爬蟲中scrapy如何處理項目數(shù)據(jù)內容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網(wǎng)!
相關文章:
1. 解決Android Studio 格式化 Format代碼快捷鍵問題2. php解決注冊并發(fā)問題并提高QPS3. 完美解決vue 中多個echarts圖表自適應的問題4. JavaEE SpringMyBatis是什么? 它和Hibernate的區(qū)別及如何配置MyBatis5. Springboot 全局日期格式化處理的實現(xiàn)6. Java使用Tesseract-Ocr識別數(shù)字7. SpringBoot+TestNG單元測試的實現(xiàn)8. vue實現(xiàn)web在線聊天功能9. 在Chrome DevTools中調試JavaScript的實現(xiàn)10. Python使用urlretrieve實現(xiàn)直接遠程下載圖片的示例代碼
