偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

手把手教你寫網(wǎng)絡(luò)爬蟲(4):Scrapy入門

開發(fā) 后端
本文介紹Scrapy的架構(gòu),包括組件以及在系統(tǒng)中發(fā)生的數(shù)據(jù)流的概覽(紅色箭頭所示)。 之后會(huì)對(duì)每個(gè)組件做簡(jiǎn)單介紹,數(shù)據(jù)流也會(huì)做一個(gè)簡(jiǎn)要描述。

本系列:

上期我們理性的分析了為什么要學(xué)習(xí)Scrapy,理由只有一個(gè),那就是免費(fèi),一分錢都不用花!

咦?怎么有人扔西紅柿?好吧,我承認(rèn)電視看多了。不過今天是沒得看了,為了趕稿,又是一個(gè)不眠夜。。。言歸正傳,我們將在這一期介紹完Scrapy的基礎(chǔ)知識(shí), 如果想深入研究,大家可以參考官方文檔,那可是出了名的全面,我就不占用公眾號(hào)的篇幅了。

[[229476]]

架構(gòu)簡(jiǎn)介

下面是Scrapy的架構(gòu),包括組件以及在系統(tǒng)中發(fā)生的數(shù)據(jù)流的概覽(紅色箭頭所示)。 之后會(huì)對(duì)每個(gè)組件做簡(jiǎn)單介紹,數(shù)據(jù)流也會(huì)做一個(gè)簡(jiǎn)要描述。

架構(gòu)就是這樣,流程和我第二篇里介紹的迷你架構(gòu)差不多,但擴(kuò)展性非常強(qiáng)大。

One more thing

[[229477]]

 

  1. scrapy startproject tutorial 

該命令將會(huì)創(chuàng)建包含下列內(nèi)容的 tutorial 目錄:

 

  1. tutorial/  
  2.     scrapy.cfg            # 項(xiàng)目的配置文件  
  3.     tutorial/             # 該項(xiàng)目的python模塊。之后您將在此加入代碼  
  4.         __init__.py  
  5.         items.py          # 項(xiàng)目中的item文件  
  6.         pipelines.py      # 項(xiàng)目中的pipelines文件  
  7.         settings.py       # 項(xiàng)目的設(shè)置文件  
  8.         spiders/          # 放置spider代碼的目錄  
  9.             __init__.py 

編寫***個(gè)爬蟲

Spider是用戶編寫用于從單個(gè)網(wǎng)站(或者一些網(wǎng)站)爬取數(shù)據(jù)的類。其包含了一個(gè)用于下載的初始URL,以及如何跟進(jìn)網(wǎng)頁中的鏈接以及如何分析頁面中的內(nèi)容的方法。

以下為我們的***個(gè)Spider代碼,保存在 tutorial/spiders 目錄下的 quotes_spider.py文件中:

 

  1. import scrapy   
  2.  
  3. class QuotesSpider(scrapy.Spider):  
  4.     name = "quotes"   
  5.  
  6.     def start_requests(self):  
  7.         urls = [  
  8.             'http://quotes.toscrape.com/page/1/' 
  9.             'http://quotes.toscrape.com/page/2/' 
  10.         ]  
  11.         for url in urls:  
  12.             yield scrapy.Request(url=url, callback=self.parse)   
  13.  
  14.     def parse(self, response):  
  15.         page = response.url.split("/")[-2]  
  16.         filename = 'quotes-%s.html' % page  
  17.         with open(filename, 'wb'as f:  
  18.             f.write(response.body)  
  19.         self.log('Saved file %s' % filename) 

運(yùn)行我們的爬蟲

進(jìn)入項(xiàng)目的根目錄,執(zhí)行下列命令啟動(dòng)spider:

 

  1. scrapy crawl quotes 

這個(gè)命令啟動(dòng)用于爬取 quotes.toscrape.com 的spider,你將得到類似的輸出:

 

  1. 2017-05-10 20:36:17 [scrapy.core.engine] INFO: Spider opened  
  2. 2017-05-10 20:36:17 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min 
  3. 2017-05-10 20:36:17 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023  
  4. 2017-05-10 20:36:17 [scrapy.core.engine] DEBUG: Crawled (404) <GET http://quotes.toscrape.com/robots.txt> (referer: None)  
  5. 2017-05-10 20:36:17 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://quotes.toscrape.com/page/1/> (referer: None)  
  6. 2017-05-10 20:36:17 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://quotes.toscrape.com/page/2/> (referer: None)  
  7. 2017-05-10 20:36:17 [quotes] DEBUG: Saved file quotes-1.html  
  8. 2017-05-10 20:36:17 [quotes] DEBUG: Saved file quotes-2.html  
  9. 2017-05-10 20:36:17 [scrapy.core.engine] INFO: Closing spider (finished) 

提取數(shù)據(jù)

我們之前只是保存了HTML頁面,并沒有提取數(shù)據(jù)?,F(xiàn)在升級(jí)一下代碼,把提取功能加進(jìn)去。至于如何使用瀏覽器的開發(fā)者模式分析網(wǎng)頁,之前已經(jīng)介紹過了。

 

  1. import scrapy   
  2. class QuotesSpider(scrapy.Spider):  
  3.     name = "quotes"  
  4.     start_urls = [  
  5.         'http://quotes.toscrape.com/page/1/' 
  6.         'http://quotes.toscrape.com/page/2/' 
  7.     ]   
  8.  
  9.     def parse(self, response):  
  10.         for quote in response.css('div.quote'):  
  11.             yield {  
  12.                 'text': quote.css('span.text::text').extract_first(),  
  13.                 'author': quote.css('small.author::text').extract_first(),  
  14.                 'tags': quote.css('div.tags a.tag::text').extract(),  
  15.             } 

再次運(yùn)行這個(gè)爬蟲,你將在日志里看到被提取出的數(shù)據(jù):

 

  1. 2017-05-10 20:38:33 [scrapy.core.scraper] DEBUG: Scraped from <200 http://quotes.toscrape.com/page/1/>  
  2. {'tags': ['life''love'], 'author''André Gide''text''“It is better to be hated for what you are than to be loved for what you are not.”' 
  3. 2017-05-10 20:38:33 [scrapy.core.scraper] DEBUG: Scraped from <200 http://quotes.toscrape.com/page/1/>  
  4. {'tags': ['edison''failure''inspirational''paraphrased'], 'author''Thomas A. Edison''text'"“I have not failed. I've just found 10,000 ways that won't work.”"

保存爬取的數(shù)據(jù)

最簡(jiǎn)單存儲(chǔ)爬取的數(shù)據(jù)的方式是使用 Feed exports:

 

  1. scrapy crawl quotes -o quotes.json 

該命令將采用 JSON 格式對(duì)爬取的數(shù)據(jù)進(jìn)行序列化,生成quotes.json文件。

 

在類似本篇教程里這樣小規(guī)模的項(xiàng)目中,這種存儲(chǔ)方式已經(jīng)足夠。如果需要對(duì)爬取到的item做更多更為復(fù)雜的操作,你可以編寫 Item Pipeline,tutorial/pipelines.py在最開始的時(shí)候已經(jīng)自動(dòng)創(chuàng)建了。 

責(zé)任編輯:龐桂玉 來源: Python開發(fā)者
相關(guān)推薦

2018-05-16 15:46:06

Python網(wǎng)絡(luò)爬蟲PhantomJS

2021-01-30 10:37:18

ScrapyGerapy網(wǎng)絡(luò)爬蟲

2018-05-22 15:30:30

Python網(wǎng)絡(luò)爬蟲分布式爬蟲

2018-05-14 16:34:08

Python網(wǎng)絡(luò)爬蟲Scrapy

2018-05-14 15:27:06

Python網(wǎng)絡(luò)爬蟲爬蟲架構(gòu)

2018-05-22 16:28:46

Python網(wǎng)絡(luò)爬蟲URL去重

2018-05-14 14:02:41

Python爬蟲網(wǎng)易云音樂

2020-07-10 08:24:18

Python開發(fā)工具

2023-03-27 08:28:57

spring代碼,starter

2011-01-10 14:41:26

2025-05-07 00:31:30

2011-05-03 15:59:00

黑盒打印機(jī)

2021-07-14 09:00:00

JavaFX開發(fā)應(yīng)用

2021-06-29 12:27:19

Spring BootCAS 登錄

2011-02-22 13:46:27

微軟SQL.NET

2021-02-26 11:54:38

MyBatis 插件接口

2021-12-28 08:38:26

Linux 中斷喚醒系統(tǒng)Linux 系統(tǒng)

2022-07-27 08:16:22

搜索引擎Lucene

2022-01-08 20:04:20

攔截系統(tǒng)調(diào)用

2022-03-14 14:47:21

HarmonyOS操作系統(tǒng)鴻蒙
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)