亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.430618.com 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

Scrapy基本介紹

scrapy是一種用于爬蟲的框架,并提供了相當成熟的模板,大大減少了程序員在編寫爬蟲時的勞動需要。

Command line tool & Project structure

使用scrapy需要先創建scrapy project,之后再于project文件夾路徑下生成spider(爬蟲)文件,編寫完程序后,再運行爬蟲(手動指定保存文件)。以上過程由命令行執行,具體如下:

  1. scrapy startproject <myproject>
  2. scrapy genspider <spider_name> <domain>
  3. scrapy crawl <spider_name> [-o filename]

后面兩個命令均要在myproject文件夾(第一個myproject)路徑下執行。而由第一個命令創建的scrapy項目結構如下:

myproject/
    scrapy.cfg
    myproject/
        __init__.py
        items.py
        middlewares.py
        pipelines.py
        settings.py
        spiders/
            __init__.py
            spider_name.py

Scrapy Overview

Scrapy框架初探

 

上圖是scrapy的基本結構,易見scrapy的程序執行和數據流動是由 engine 來調度控制的,關于該結構的具體解釋見: scrapy document overview .

Scrapy Spider詳解

基礎的使用scrapy一般只需要在spider_name.py中進行編寫,該文件是我們使用scrapy genspider <spider_name>命令后自動創建中,文件中還自動import了scrapy并且還自動創建了一個模版spider類(自動繼承自scrapy.Spider)。spider的功能簡介于此: scrapy document spider 。這里介紹一些常用的scray.Spider類的屬性及方法:

Attribute :

name :name屬性即是我們使用命令行時所指定的spider_name,這是scrapy框架中用于標識spider所用,每個spider都必須有一個獨一無二的name。

allowed_domains :該屬性為一個以string為元素的list,每個元素string為一個domain,限定爬蟲所能爬取的網址。

start_urls :該屬性同樣為一個以string為元素的list,每個元素string對應一個完整的url,是spider開始時需要爬取的初始網址。

custom_settings :該屬性的形式為一個dict,即如修改user-agent,開啟pipeline,指定log_level之類的,并且局限于http header。

method :

start_requests :該方法用于在scrapy開始爬蟲時以start_urls返回一個Request iterable或者一個generator,該方法在一次爬蟲過程中僅被調用一次。其默認實現為:Request(url, dont_filter=True) for each url in start_urls。

parse :這是spider類中最重要的一個方法。由scrapy文檔中對spider的介紹,spider每發出一個Request,便要對該Request的Response進行處理,處理的函數稱為callback function,即 one Request corresponds to one Callback。而parse就是默認的callback函數,它負責對傳回的response進行解析(xpath),提取數據(item dict-like object)并交予,并且還會根據需要發出新的Request請求。和start_requests一樣,它的返回值也需要是一個iterable或是一個generator。一般來說,先yield item,再根據對response的解析得到新的url與yield Request(url,callback)。這里對于response的解析和數據提取交予過程略過,具體可以見于 b站教程 。

由以上介紹可見對于start_request和parse方法的返回值要求,scrapy框架的內部代碼邏輯應該是像for循環一樣輪詢兩者的返回值,對于parse方法還需要判斷其返回值的類型(item/Request)來區別處理。

Scrapy Request and Response

Typically, Request objects are generated in the spiders and pass across the system until they reach the Downloader, which executes the request and returns a Response object which travels back to the spider that issued the request.

Request object

scrapy.Request(url,callback,method="GET",headers=None,body=None,cookies=None,meta=None,..,dont_filter=False)

  • 其中url參數即為我們想要爬取網站的url
  • callback為該request對應的在返回response時的處理函數
  • method是http請求方法,常用的有:"GET","POST"
  • headers是請求頭,形式為dict
  • body是http請求的請求數據,即一般的表單數據,形式為bytes字符串
  • cookies形式為dict
  • 這里先講dont_filter,默認值為False,scrapy中默認對于同一url是不重復爬取的,所以會把相同的url給filter掉,而有時我們需要爬取同一url(如爬取某個不斷更新的論壇頁面),就需要把該值設為True
  • meta The initial values for the Request.meta attribute. If given, the dict passed in this parameter will be shallow copied.該參數也是字典形式,是為了在spider類的多個parse函數之間傳遞信息,見 知乎 。 注意Response對象也有一個它對應的Request對象 :The Request object that generated this response. This attribute is assigned in the Scrapy engine, after the response and the request have passed through all Downloader Middlewares In particular, this means that:HTTP redirections will cause the original request (to the URL before redirection) to be assigned to the redirected response (with the final URL after redirection).Response.request.url doesn’t always equal Response.urlThis attribute is only available in the spider code, and in the Spider Middlewares, but not in Downloader Middlewares (although you have the Request available there by other means) and handlers of the response_downloaded signal.But Unlike the Response.request attribute, the Response.meta attribute is propagated along redirects and retries, so you will get the original Request.meta sent from your spider.

Response obejct

這里僅介紹一些reponse對象的屬性:

  • url 即該response的來源url
  • status 即該response的狀態碼
  • headers response的響應頭,形式為dict
  • body response的相應數據體,形式為bytes
  • request response對應的Request對象,對于它上文已經介紹,即Response.url可能不等于Reponse.request.url,因為redirection的原因

Settings

Settings can be populated using different mechanisms, each of which having a different precedence. Here is the list of them in decreasing order of precedence:

  1. Command line options (most precedence)
  2. Settings per-spider
  3. Project settings module(settings.py)
  4. Default settings per-command
  5. Default global settings (less precedence)

一般我們直接在settings.py文件中對其進行修改,常見需要增改的有:user-agent指定,ITEM_PIPELINES解除注釋以開啟pipeline功能,LOG_LEVEL和LOG_FILE指定,ROBOTSTXT_OBEY設為False等等。

分享到:
標簽:框架 Scrapy
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定