溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Scrapy的基礎(chǔ)知識是什么

發(fā)布時間:2021-12-04 15:08:00 來源:億速云 閱讀:111 作者:柒染 欄目:編程語言

Scrapy的基礎(chǔ)知識是什么,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

我們將在這里介紹完Scrapy的基礎(chǔ)知識

架構(gòu)簡介

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

Scrapy的基礎(chǔ)知識是什么

Scrapy的基礎(chǔ)知識是什么

Scrapy的基礎(chǔ)知識是什么

Scrapy的基礎(chǔ)知識是什么

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

One more thing

Scrapy的基礎(chǔ)知識是什么

Scrapy的基礎(chǔ)知識是什么

Scrapy的基礎(chǔ)知識是什么

scrapy startproject tutorial

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

tutorial/      scrapy.cfg            # 項目的配置文件      tutorial/             # 該項目的python模塊。之后您將在此加入代碼          __init__.py          items.py          # 項目中的item文件          pipelines.py      # 項目中的pipelines文件          settings.py       # 項目的設置文件          spiders/          # 放置spider代碼的目錄              __init__.py

編寫第一個爬蟲

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

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

import scrapy    class QuotesSpider(scrapy.Spider):      name = "quotes"        def start_requests(self):          urls = [              'http://quotes.toscrape.com/page/1/',              'http://quotes.toscrape.com/page/2/',          ]          for url in urls:              yield scrapy.Request(url=url, callback=self.parse)        def parse(self, response):          page = response.url.split("/")[-2]          filename = 'quotes-%s.html' % page          with open(filename, 'wb') as f:              f.write(response.body)          self.log('Saved file %s' % filename)

運行我們的爬蟲

進入項目的根目錄,執(zhí)行下列命令啟動spider:

scrapy crawl quotes

這個命令啟動用于爬取 quotes.toscrape.com 的spider,你將得到類似的輸出:

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

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

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

import scrapy   class QuotesSpider(scrapy.Spider):      name = "quotes"      start_urls = [          'http://quotes.toscrape.com/page/1/',          'http://quotes.toscrape.com/page/2/',      ]        def parse(self, response):          for quote in response.css('div.quote'):              yield {                  'text': quote.css('span.text::text').extract_first(),                  'author': quote.css('small.author::text').extract_first(),                  'tags': quote.css('div.tags a.tag::text').extract(),              }

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

2017-05-10 20:38:33 [scrapy.core.scraper] DEBUG: Scraped from <200 http://quotes.toscrape.com/page/1/>  {'tags': ['life', 'love'], 'author': 'Andr&eacute; Gide', 'text': '“It is better to be hated for what you are than to be loved for what you are not.”'}  2017-05-10 20:38:33 [scrapy.core.scraper] DEBUG: Scraped from <200 http://quotes.toscrape.com/page/1/>  {'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ù)

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

scrapy crawl quotes -o quotes.json

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

如果需要對爬取到的item做更多更為復雜的操作,你可以編寫 Item Pipeline,tutorial/pipelines.py在最開始的時候已經(jīng)自動創(chuàng)建了。

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI