<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 請求和響應 > 譯者:[OSGeo 中國](https://www.osgeo.cn/) 零星用途 [`Request`](#scrapy.http.Request "scrapy.http.Request") 和 [`Response`](#scrapy.http.Response "scrapy.http.Response") 用于對網站進行爬網的對象。 通常, [`Request`](#scrapy.http.Request "scrapy.http.Request") 對象在spider中生成并在系統中傳遞,直到它們到達下載程序,下載程序執行請求并返回 [`Response`](#scrapy.http.Response "scrapy.http.Response") 返回發出請求的spider的對象。 兩個 [`Request`](#scrapy.http.Request "scrapy.http.Request") 和 [`Response`](#scrapy.http.Response "scrapy.http.Response") 類具有子類,這些子類添加了基類中不需要的功能。這些在下面的 [請求子類](#topics-request-response-ref-request-subclasses) 和 [響應子類](#topics-request-response-ref-response-subclasses) . ## 請求對象 ```py class scrapy.http.Request(url[, callback, method='GET', headers, body, cookies, meta, encoding='utf-8', priority=0, dont_filter=False, errback, flags]) ``` A [`Request`](#scrapy.http.Request "scrapy.http.Request") 對象表示一個HTTP請求,通常由spider生成并由下載程序執行,從而生成一個 [`Response`](#scrapy.http.Response "scrapy.http.Response") . | 參數: | * **url** (_string_) -- 此請求的URL * **callback** (_callable_) -- 將以此請求的響應(一旦下載)作為第一個參數調用的函數。有關詳細信息,請參閱 [向回調函數傳遞附加數據](#topics-request-response-ref-request-callback-arguments) 下面。如果請求未指定回調,則蜘蛛 [`parse()`](spiders.html#scrapy.spiders.Spider.parse "scrapy.spiders.Spider.parse") 將使用方法。請注意,如果在處理過程中引發異常,則改為調用errback。 * **method** (_string_) -- 此請求的HTTP方法。默認為 `'GET'` . * **meta** (_dict_) -- 的初始值 [`Request.meta`](#scrapy.http.Request.meta "scrapy.http.Request.meta") 屬性。如果給定,則將淺復制傳入此參數的dict。 * **body** (_str_ _or_ _unicode_) -- 請求主體。如果A `unicode` 傳遞,然后將其編碼為 `str` 使用 `encoding` 通過(默認為 `utf-8` )如果 `body` 如果未給定,則存儲空字符串。無論此參數的類型如何,存儲的最終值都將是 `str` (永遠) `unicode` 或 `None` ) * **headers** (_dict_) -- 此請求的頭。dict值可以是字符串(對于單值頭)或列表(對于多值頭)。如果 `None` 作為值傳遞,HTTP頭將不會被發送。 * **cookies** (_dict_ _or_ _list_) -- 請求cookies。這些可以用兩種形式發送。1。使用dict::request_with_cookies=request(url=“[http://www.example.com](http://www.example.com)”,cookies=貨幣“:'usd”,country“:'uy”)2。使用dicts::request_with_cookies=request(url=“[http://www.example.com](http://www.example.com)”,cookies)列表= [{{'name': 'currency', 'value': 'USD', 'domain': 'example.com', 'path': '/currency'}}] )后一個表單允許自定義 `domain` 和 `path` cookie的屬性。只有在為以后的請求保存cookie時,這才有用。…reqmeta::當某些站點返回cookie(響應中)時,不要合并cookie。這些cookie存儲在該域的cookie中,并將在將來的請求中再次發送。這是任何常規Web瀏覽器的典型行為。但是,如果出于某種原因,您希望避免與現有cookie合并,您可以通過設置 `dont_merge_cookies` 中的“真”鍵 [`Request.meta`](#scrapy.http.Request.meta "scrapy.http.Request.meta") . 不合并cookies的請求示例::request_with_cookies=request(url=“[http://www.example.com](http://www.example.com)”,cookies=currency':'usd','country':'uy',meta=不合并_cookies':true)有關詳細信息,請參閱 [CookiesMiddleware](downloader-middleware.html#cookies-mw) . * **encoding** (_string_) -- 此請求的編碼(默認為 `'utf-8'` )此編碼將用于對URL進行百分比編碼并將正文轉換為 `str` (如果給予 `unicode` ) * **priority** (_int_) -- 此請求的優先級(默認為 `0` )調度程序使用優先級定義用于處理請求的順序。優先級值較高的請求將更早執行。允許負值以表示相對較低的優先級。 * **dont_filter** (_boolean_) -- 指示調度程序不應篩選此請求。當您希望多次執行相同的請求時,可以使用此選項忽略重復的篩選器。小心使用,否則會進入爬行循環。默認為 `False` . * **errback** (_callable_) -- 如果在處理請求時引發任何異常,則將調用的函數。這包括404 HTTP錯誤等失敗的頁面。它收到一個 [Twisted Failure](https://twistedmatrix.com/documents/current/api/twisted.python.failure.Failure.html) 實例作為第一個參數。有關詳細信息,請參閱 [使用errbacks捕獲請求處理中的異常](#topics-request-response-ref-errbacks) 下面。 * **flags** (_list_) -- 發送到請求的標志可用于日志記錄或類似用途。 | | --- | --- | ```py url ``` 包含此請求的URL的字符串。請記住,此屬性包含轉義的URL,因此它可以不同于構造函數中傳遞的URL。 此屬性是只讀的。要更改請求的URL,請使用 [`replace()`](#scrapy.http.Request.replace "scrapy.http.Request.replace") . ```py method ``` 表示請求中HTTP方法的字符串。這保證是大寫的。例子: `"GET"` , `"POST"` , `"PUT"` 等 ```py headers ``` 包含請求頭的類似字典的對象。 ```py body ``` 包含請求主體的str。 此屬性是只讀的。要更改請求正文,請使用 [`replace()`](#scrapy.http.Request.replace "scrapy.http.Request.replace") . ```py meta ``` 包含此請求的任意元數據的dict。對于新請求,此dict是空的,通常由不同的零碎組件(擴展、中間產品等)填充。所以這個dict中包含的數據取決于您啟用的擴展名。 見 [請求.meta特殊鍵](#topics-request-meta) 獲取scrapy識別的特殊元鍵列表。 這個字典是 [shallow copied](https://docs.python.org/2/library/copy.html) 當使用 `copy()` 或 `replace()` 方法,也可以通過 `response.meta` 屬性。 ```py copy() ``` 返回一個新請求,它是此請求的副本。參見: [向回調函數傳遞附加數據](#topics-request-response-ref-request-callback-arguments) . ```py replace([url, method, headers, body, cookies, meta, encoding, dont_filter, callback, errback]) ``` 返回具有相同成員的請求對象,除了那些通過指定的關鍵字參數賦予新值的成員。屬性 [`Request.meta`](#scrapy.http.Request.meta "scrapy.http.Request.meta") 默認情況下復制(除非在 `meta` 爭論)。也見 [向回調函數傳遞附加數據](#topics-request-response-ref-request-callback-arguments) . ### 向回調函數傳遞附加數據 請求的回調是一個函數,在下載請求的響應時將調用該函數。將使用下載的 [`Response`](#scrapy.http.Response "scrapy.http.Response") 對象作為其第一個參數。 例子:: ```py def parse_page1(self, response): return scrapy.Request("http://www.example.com/some_page.html", callback=self.parse_page2) def parse_page2(self, response): # this would log http://www.example.com/some_page.html self.logger.info("Visited %s", response.url) ``` 在某些情況下,您可能對向這些回調函數傳遞參數感興趣,以便稍后在第二個回調中接收這些參數。你可以使用 [`Request.meta`](#scrapy.http.Request.meta "scrapy.http.Request.meta") 屬性。 下面是一個示例,說明如何使用此機制傳遞一個項,以填充來自不同頁面的不同字段: ```py def parse_page1(self, response): item = MyItem() item['main_url'] = response.url request = scrapy.Request("http://www.example.com/some_page.html", callback=self.parse_page2) request.meta['item'] = item yield request def parse_page2(self, response): item = response.meta['item'] item['other_url'] = response.url yield item ``` ### 使用errbacks捕獲請求處理中的異常 請求的errback是一個函數,在處理異常時將調用該函數。 它收到一個 [Twisted Failure](https://twistedmatrix.com/documents/current/api/twisted.python.failure.Failure.html) 實例作為第一個參數,可用于跟蹤連接建立超時、DNS錯誤等。 下面是一個spider示例,記錄所有錯誤,并在需要時捕獲一些特定錯誤: ```py import scrapy from scrapy.spidermiddlewares.httperror import HttpError from twisted.internet.error import DNSLookupError from twisted.internet.error import TimeoutError, TCPTimedOutError class ErrbackSpider(scrapy.Spider): name = "errback_example" start_urls = [ "http://www.httpbin.org/", # HTTP 200 expected "http://www.httpbin.org/status/404", # Not found error "http://www.httpbin.org/status/500", # server issue "http://www.httpbin.org:12345/", # non-responding host, timeout expected "http://www.httphttpbinbin.org/", # DNS error expected ] def start_requests(self): for u in self.start_urls: yield scrapy.Request(u, callback=self.parse_httpbin, errback=self.errback_httpbin, dont_filter=True) def parse_httpbin(self, response): self.logger.info('Got successful response from {}'.format(response.url)) # do something useful here... def errback_httpbin(self, failure): # log all failures self.logger.error(repr(failure)) # in case you want to do something special for some errors, # you may need the failure's type: if failure.check(HttpError): # these exceptions come from HttpError spider middleware # you can get the non-200 response response = failure.value.response self.logger.error('HttpError on %s', response.url) elif failure.check(DNSLookupError): # this is the original request request = failure.request self.logger.error('DNSLookupError on %s', request.url) elif failure.check(TimeoutError, TCPTimedOutError): request = failure.request self.logger.error('TimeoutError on %s', request.url) ``` ## 請求.meta特殊鍵 這個 [`Request.meta`](#scrapy.http.Request.meta "scrapy.http.Request.meta") 屬性可以包含任意數據,但有一些特殊的鍵可以被scrapy及其內置擴展識別。 那些是: * [`dont_redirect`](downloader-middleware.html#std:reqmeta-dont_redirect) * [`dont_retry`](downloader-middleware.html#std:reqmeta-dont_retry) * [`handle_httpstatus_list`](spider-middleware.html#std:reqmeta-handle_httpstatus_list) * [`handle_httpstatus_all`](spider-middleware.html#std:reqmeta-handle_httpstatus_all) * [`dont_merge_cookies`](#std:reqmeta-dont_merge_cookies) * [`cookiejar`](downloader-middleware.html#std:reqmeta-cookiejar) * [`dont_cache`](downloader-middleware.html#std:reqmeta-dont_cache) * [`redirect_reasons`](downloader-middleware.html#std:reqmeta-redirect_reasons) * [`redirect_urls`](downloader-middleware.html#std:reqmeta-redirect_urls) * [`bindaddress`](#std:reqmeta-bindaddress) * [`dont_obey_robotstxt`](downloader-middleware.html#std:reqmeta-dont_obey_robotstxt) * [`download_timeout`](#std:reqmeta-download_timeout) * [`download_maxsize`](settings.html#std:reqmeta-download_maxsize) * [`download_latency`](#std:reqmeta-download_latency) * [`download_fail_on_dataloss`](#std:reqmeta-download_fail_on_dataloss) * [`proxy`](downloader-middleware.html#std:reqmeta-proxy) * `ftp_user` (見 [`FTP_USER`](settings.html#std:setting-FTP_USER) 更多信息) * `ftp_password` (見 [`FTP_PASSWORD`](settings.html#std:setting-FTP_PASSWORD) 更多信息) * [`referrer_policy`](spider-middleware.html#std:reqmeta-referrer_policy) * [`max_retry_times`](#std:reqmeta-max_retry_times) ### 綁定地址 用于執行請求的傳出IP地址的IP。 ### download_timeout 下載程序在超時前等待的時間(以秒計)。參見: [`DOWNLOAD_TIMEOUT`](settings.html#std:setting-DOWNLOAD_TIMEOUT) . ### download_latency 自請求啟動以來,獲取響應所花費的時間,即通過網絡發送的HTTP消息。只有在下載響應后,此元鍵才可用。雖然大多數其他的元鍵用于控制零碎的行為,但這個元鍵應該是只讀的。 ### download_fail_on_dataloss 是否在錯誤的響應上失敗。見: [`DOWNLOAD_FAIL_ON_DATALOSS`](settings.html#std:setting-DOWNLOAD_FAIL_ON_DATALOSS) . ### max_retry_times 使用meta key設置每個請求的重試次數。初始化時, [`max_retry_times`](#std:reqmeta-max_retry_times) 元鍵優先于 [`RETRY_TIMES`](downloader-middleware.html#std:setting-RETRY_TIMES) 設置。 ## 請求子類 這是內置的列表 [`Request`](#scrapy.http.Request "scrapy.http.Request") 子類。您還可以將其子類化,以實現您自己的自定義功能。 ### FormRequest對象 FormRequest類擴展了基 [`Request`](#scrapy.http.Request "scrapy.http.Request") 具有處理HTML表單的功能。它使用 [lxml.html forms](http://lxml.de/lxmlhtml.html#forms) 使用表單數據預填充表單域的步驟 [`Response`](#scrapy.http.Response "scrapy.http.Response") 物體。 ```py class scrapy.http.FormRequest(url[, formdata, ...]) ``` 這個 [`FormRequest`](#scrapy.http.FormRequest "scrapy.http.FormRequest") 類向構造函數添加新參數。其余參數與 [`Request`](#scrapy.http.Request "scrapy.http.Request") 在這里沒有記錄。 | 參數: | **formdata** (_dict_ _or_ _iterable of tuples_) -- 是包含HTML表單數據的字典(或可為(鍵、值)元組),這些數據將被URL編碼并分配給請求主體。 | | --- | --- | 這個 [`FormRequest`](#scrapy.http.FormRequest "scrapy.http.FormRequest") 除了標準之外,對象還支持以下類方法 [`Request`](#scrapy.http.Request "scrapy.http.Request") 方法: ```py classmethod from_response(response[, formname=None, formid=None, formnumber=0, formdata=None, formxpath=None, formcss=None, clickdata=None, dont_click=False, ...]) ``` 返回新的 [`FormRequest`](#scrapy.http.FormRequest "scrapy.http.FormRequest") 對象,其表單字段值預填充在HTML中 `&lt;form&gt;` 包含在給定響應中的元素。有關示例,請參見 [使用formRequest.from_response()模擬用戶登錄](#topics-request-response-ref-request-userlogin) . 默認情況下,策略是在任何看起來可單擊的窗體控件上自動模擬單擊,如 `&lt;input type="submit"&gt;` . 盡管這非常方便,而且常常是所需的行為,但有時它可能會導致難以調試的問題。例如,當處理使用javascript填充和/或提交的表單時,默認 [`from_response()`](#scrapy.http.FormRequest.from_response "scrapy.http.FormRequest.from_response") 行為可能不是最合適的。要禁用此行為,可以設置 `dont_click` 參數 `True` . 此外,如果要更改單擊的控件(而不是禁用它),還可以使用 `clickdata` 爭論。 警告 對于選項值中有前導空格或尾隨空格的select元素,使用此方法將不起作用,因為 [bug in lxml](https://bugs.launchpad.net/lxml/+bug/1665241) ,應在LXML 3.8及更高版本中修復。 | 參數: | * **response** ([`Response`](#scrapy.http.Response "scrapy.http.Response") object) -- 包含用于預填充表單字段的HTML表單的響應 * **formname** (_string_) -- 如果給定,將使用名稱屬性設置為該值的表單。 * **formid** (_string_) -- 如果給定,將使用ID屬性設置為該值的表單。 * **formxpath** (_string_) -- 如果給定,將使用與xpath匹配的第一個表單。 * **formcss** (_string_) -- 如果給定,將使用與CSS選擇器匹配的第一個表單。 * **formnumber** (_integer_) -- 當響應包含多個表單時要使用的表單數。第一個(也是默認值)是 `0` . * **formdata** (_dict_) -- 要在表單數據中重寫的字段。如果響應中已存在字段 `&lt;form&gt;` 元素,其值將被此參數中傳遞的值重寫。如果此參數中傳遞的值是 `None` ,即使響應中存在該字段,該字段也不會包含在請求中。 `&lt;form&gt;` 元素。 * **clickdata** (_dict_) -- 用于查找單擊的控件的屬性。如果沒有給出,將提交表單數據,模擬單擊第一個可單擊元素。除了HTML屬性之外,控件還可以通過其相對于表單內其他可提交輸入的基于零的索引進行標識,方法是 `nr` 屬性。 * **dont_click** (_boolean_) -- 如果為真,則表單數據將在不單擊任何元素的情況下提交。 | | --- | --- | 該類方法的其他參數直接傳遞給 [`FormRequest`](#scrapy.http.FormRequest "scrapy.http.FormRequest") 建造師。 0.10.3 新版功能: 這個 `formname` 參數。 0.17 新版功能: 這個 `formxpath` 參數。 1.1.0 新版功能: 這個 `formcss` 參數。 1.1.0 新版功能: 這個 `formid` 參數。 ### 請求使用示例 #### 使用FormRequest通過HTTP Post發送數據 如果您想在spider中模擬HTML表單發布并發送幾個鍵值字段,可以返回 [`FormRequest`](#scrapy.http.FormRequest "scrapy.http.FormRequest") 像這樣的物體: ```py return [FormRequest(url="http://www.example.com/post/action", formdata={'name': 'John Doe', 'age': '27'}, callback=self.after_post)] ``` #### 使用formRequest.from_response()模擬用戶登錄 網站通常通過 `&lt;input type="hidden"&gt;` 元素,例如與會話相關的數據或身份驗證令牌(用于登錄頁)。當進行抓取時,您將希望這些字段自動預填充,并且只覆蓋其中的幾個字段,例如用戶名和密碼。你可以使用 [`FormRequest.from_response()`](#scrapy.http.FormRequest.from_response "scrapy.http.FormRequest.from_response") 此作業的方法。下面是一個蜘蛛的例子,它使用它: ```py import scrapy def authentication_failed(response): # TODO: Check the contents of the response and return True if it failed # or False if it succeeded. pass class LoginSpider(scrapy.Spider): name = 'example.com' start_urls = ['http://www.example.com/users/login.php'] def parse(self, response): return scrapy.FormRequest.from_response( response, formdata={'username': 'john', 'password': 'secret'}, callback=self.after_login ) def after_login(self, response): if authentication_failed(response): self.logger.error("Login failed") return # continue scraping with authenticated session... ``` ### JSONRequest jsonRequest類擴展了基 [`Request`](#scrapy.http.Request "scrapy.http.Request") 類,具有處理JSON請求的功能。 ```py class scrapy.http.JSONRequest(url[, ... data, dumps_kwargs]) ``` 這個 [`JSONRequest`](#scrapy.http.JSONRequest "scrapy.http.JSONRequest") 類向構造函數添加兩個新參數。其余參數與 [`Request`](#scrapy.http.Request "scrapy.http.Request") 在這里沒有記錄。 使用 [`JSONRequest`](#scrapy.http.JSONRequest "scrapy.http.JSONRequest") 將設置 `Content-Type` 報頭到 `application/json` 和 `Accept` 報頭到 `application/json, text/javascript, */*; q=0.01` | 參數: | * **data** (_JSON serializable object_) -- 是需要對JSON編碼并分配給主體的任何JSON可序列化對象。如果 [`Request.body`](#scrapy.http.Request.body "scrapy.http.Request.body") 提供了參數。此參數將被忽略。如果 [`Request.body`](#scrapy.http.Request.body "scrapy.http.Request.body") 未提供參數,并且提供了數據參數 [`Request.method`](#scrapy.http.Request.method "scrapy.http.Request.method") 將被設置為 `'POST'` 自動地。 * **dumps_kwargs** (_dict_) -- 將傳遞給基礎的參數 [json.dumps](https://docs.python.org/3/library/json.html#json.dumps) 方法,用于將數據序列化為JSON格式。 | | --- | --- | ### json請求使用示例 使用JSON負載發送JSON POST請求: ```py data = { 'name1': 'value1', 'name2': 'value2', } yield JSONRequest(url='http://www.example.com/post/action', data=data) ``` ## 響應對象 ```py class scrapy.http.Response(url[, status=200, headers=None, body=b'', flags=None, request=None]) ``` A [`Response`](#scrapy.http.Response "scrapy.http.Response") 對象表示一個HTTP響應,它通常被下載(由下載程序)并送入spider進行處理。 | 參數: | * **url** (_string_) -- 此響應的URL * **status** (_integer_) -- 響應的HTTP狀態。默認為 `200` . * **headers** (_dict_) -- 此響應的頭。dict值可以是字符串(對于單值頭)或列表(對于多值頭)。 * **body** (_bytes_) -- 響應主體。要以str(python 2中的unicode)形式訪問解碼后的文本,可以使用 `response.text` 從編碼感知 [Response subclass](#topics-request-response-ref-response-subclasses) ,如 [`TextResponse`](#scrapy.http.TextResponse "scrapy.http.TextResponse") . * **flags** (_list_) -- 是一個列表,其中包含 [`Response.flags`](#scrapy.http.Response.flags "scrapy.http.Response.flags") 屬性。如果給定,則將淺復制列表。 * **request** ([`Request`](#scrapy.http.Request "scrapy.http.Request") object) -- 的初始值 [`Response.request`](#scrapy.http.Response.request "scrapy.http.Response.request") 屬性。這代表 [`Request`](#scrapy.http.Request "scrapy.http.Request") 產生了這個響應。 | | --- | --- | ```py url ``` 包含響應的URL的字符串。 此屬性是只讀的。要更改響應的URL,請使用 [`replace()`](#scrapy.http.Response.replace "scrapy.http.Response.replace") . ```py status ``` 表示響應的HTTP狀態的整數。例子: `200` , `404` . ```py headers ``` 包含響應頭的類似字典的對象。可以使用訪問值 `get()` 返回具有指定名稱的第一個頭值,或 `getlist()` 返回具有指定名稱的所有頭值。例如,此調用將為您提供標題中的所有cookie:: ```py response.headers.getlist('Set-Cookie') ``` ```py body ``` 這個反應的主體。記住response.body始終是一個bytes對象。如果要使用Unicode版本,請使用 [`TextResponse.text`](#scrapy.http.TextResponse.text "scrapy.http.TextResponse.text") (僅在 [`TextResponse`](#scrapy.http.TextResponse "scrapy.http.TextResponse") 和子類)。 此屬性是只讀的。要更改響應主體,請使用 [`replace()`](#scrapy.http.Response.replace "scrapy.http.Response.replace") . ```py request ``` 這個 [`Request`](#scrapy.http.Request "scrapy.http.Request") 生成此響應的對象。在響應和請求通過所有 [Downloader Middlewares](downloader-middleware.html#topics-downloader-middleware) . 特別是,這意味著: * HTTP重定向將導致將原始請求(重定向前的URL)分配給重定向響應(重定向后的最終URL)。 * response.request.url并不總是等于response.url * 此屬性僅在spider代碼和 [Spider Middlewares](spider-middleware.html#topics-spider-middleware) ,但在下載器中間軟件(盡管您通過其他方式有可用的請求)和 [`response_downloaded`](signals.html#std:signal-response_downloaded) 信號。 ```py meta ``` 到的快捷方式 [`Request.meta`](#scrapy.http.Request.meta "scrapy.http.Request.meta") 的屬性 [`Response.request`](#scrapy.http.Response.request "scrapy.http.Response.request") 對象(I. `self.request.meta` ) 不像 [`Response.request`](#scrapy.http.Response.request "scrapy.http.Response.request") 屬性 [`Response.meta`](#scrapy.http.Response.meta "scrapy.http.Response.meta") 屬性是沿著重定向和重試傳播的,因此您將獲得原始的 [`Request.meta`](#scrapy.http.Request.meta "scrapy.http.Request.meta") 從你的蜘蛛那里送來的。 參見 [`Request.meta`](#scrapy.http.Request.meta "scrapy.http.Request.meta") 屬性 ```py flags ``` 包含此響應標志的列表。標記是用于標記響應的標簽。例如: `'cached'` , `'redirected` '等,它們顯示在響應的字符串表示形式上 (&lt;cite&gt;__str__&lt;/cite&gt; 方法)由引擎用于日志記錄。 ```py copy() ``` 返回此響應的副本的新響應。 ```py replace([url, status, headers, body, request, flags, cls]) ``` 返回具有相同成員的響應對象,除了那些通過指定的關鍵字參數賦予新值的成員。屬性 [`Response.meta`](#scrapy.http.Response.meta "scrapy.http.Response.meta") 默認情況下是復制的。 ```py urljoin(url) ``` 通過組合響應的 [`url`](#scrapy.http.Response.url "scrapy.http.Response.url") 有一個可能的相對URL。 這是包裝紙 [urlparse.urljoin](https://docs.python.org/2/library/urlparse.html#urlparse.urljoin) ,這僅僅是進行此呼叫的別名: ```py urlparse.urljoin(response.url, url) ``` ```py follow(url, callback=None, method='GET', headers=None, body=None, cookies=None, meta=None, encoding='utf-8', priority=0, dont_filter=False, errback=None) ``` 返回A [`Request`](#scrapy.http.Request "scrapy.http.Request") 要跟蹤鏈接的實例 `url` . 它接受與 `Request.__init__` 方法,但 `url` 可以是相對URL或 `scrapy.link.Link` 對象,而不僅僅是絕對URL。 [`TextResponse`](#scrapy.http.TextResponse "scrapy.http.TextResponse") 提供了一個 [`follow()`](#scrapy.http.TextResponse.follow "scrapy.http.TextResponse.follow") 方法,它除了支持絕對/相對URL和鏈接對象之外還支持選擇器。 ## 響應子類 下面是可用的內置響應子類的列表。您還可以對響應類進行子類化,以實現您自己的功能。 ### 文本響應對象 ```py class scrapy.http.TextResponse(url[, encoding[, ...]]) ``` [`TextResponse`](#scrapy.http.TextResponse "scrapy.http.TextResponse") 對象將編碼功能添加到基 [`Response`](#scrapy.http.Response "scrapy.http.Response") 類,它只用于二進制數據,如圖像、聲音或任何媒體文件。 [`TextResponse`](#scrapy.http.TextResponse "scrapy.http.TextResponse") 對象除了支持基參數外,還支持新的構造函數參數 [`Response`](#scrapy.http.Response "scrapy.http.Response") 物體。其余功能與 [`Response`](#scrapy.http.Response "scrapy.http.Response") 類,此處未記錄。 | 參數: | **encoding** (_string_) -- 包含用于此響應的編碼的字符串。如果創建一個 [`TextResponse`](#scrapy.http.TextResponse "scrapy.http.TextResponse") 對象具有Unicode主體,將使用此編碼對其進行編碼(記住主體屬性始終是字符串)。如果 `encoding` 是 `None` (默認值),將在響應頭和正文中查找編碼。 | | --- | --- | [`TextResponse`](#scrapy.http.TextResponse "scrapy.http.TextResponse") 除了標準之外,對象還支持以下屬性 [`Response`](#scrapy.http.Response "scrapy.http.Response") 一: ```py text ``` 響應主體,作為Unicode。 一樣 `response.body.decode(response.encoding)` ,但結果在第一次調用后緩存,因此您可以訪問 `response.text` 多次無額外開銷。 注解 `unicode(response.body)` 不是將響應正文轉換為Unicode的正確方法:您將使用系統默認編碼(通常 `ascii` )而不是響應編碼。 ```py encoding ``` 帶有此響應編碼的字符串。按順序嘗試以下機制來解決編碼問題: 1. 在構造函數中傳遞的編碼 `encoding` 論點 2. 內容類型HTTP頭中聲明的編碼。如果此編碼無效(即未知),則忽略此編碼,并嘗試下一個解決機制。 3. 響應正文中聲明的編碼。TextResponse類不為此提供任何特殊功能。然而, [`HtmlResponse`](#scrapy.http.HtmlResponse "scrapy.http.HtmlResponse") 和 [`XmlResponse`](#scrapy.http.XmlResponse "scrapy.http.XmlResponse") 上課。 4. 通過查看響應主體推斷出的編碼。這是更脆弱的方法,也是最后一個嘗試的方法。 ```py selector ``` A [`Selector`](selectors.html#scrapy.selector.Selector "scrapy.selector.Selector") 使用響應作為目標的實例。選擇器在第一次訪問時被惰性地實例化。 [`TextResponse`](#scrapy.http.TextResponse "scrapy.http.TextResponse") 對象除了支持標準之外還支持以下方法 [`Response`](#scrapy.http.Response "scrapy.http.Response") 一: ```py xpath(query) ``` 捷徑 `TextResponse.selector.xpath(query)` :: ```py response.xpath('//p') ``` ```py css(query) ``` 捷徑 `TextResponse.selector.css(query)` :: ```py response.css('p') ``` ```py follow(url, callback=None, method='GET', headers=None, body=None, cookies=None, meta=None, encoding=None, priority=0, dont_filter=False, errback=None) ``` 返回A [`Request`](#scrapy.http.Request "scrapy.http.Request") 要跟蹤鏈接的實例 `url` . 它接受與 `Request.__init__` 方法,但 `url` 不僅可以是絕對URL,而且可以是 * 相對URL; * scrappy.link.link對象(例如鏈接提取程序結果); * 屬性選擇器(非選擇器列表)-例如 `response.css('a::attr(href)')[0]` 或 `response.xpath('//img/@src')[0]` . * 選擇器 `&lt;a&gt;` 或 `&lt;link&gt;` 元素,例如 `response.css('a.my_link')[0]` . 見 [創建請求的快捷方式](../intro/tutorial.html#response-follow-example) 用于示例。 ```py body_as_unicode() ``` 一樣 [`text`](#scrapy.http.TextResponse.text "scrapy.http.TextResponse.text") ,但作為一種方法提供。此方法是為了向后兼容而保留的;請首選 `response.text` . ### HTMLResponse對象 ```py class scrapy.http.HtmlResponse(url[, ...]) ``` 這個 [`HtmlResponse`](#scrapy.http.HtmlResponse "scrapy.http.HtmlResponse") 類是的子類 [`TextResponse`](#scrapy.http.TextResponse "scrapy.http.TextResponse") 它通過查看HTML添加了編碼自動發現支持 [meta http-equiv](https://www.w3schools.com/TAGS/att_meta_http_equiv.asp) 屬性。見 [`TextResponse.encoding`](#scrapy.http.TextResponse.encoding "scrapy.http.TextResponse.encoding") . ### XmlResponse對象 ```py class scrapy.http.XmlResponse(url[, ...]) ``` 這個 [`XmlResponse`](#scrapy.http.XmlResponse "scrapy.http.XmlResponse") 類是的子類 [`TextResponse`](#scrapy.http.TextResponse "scrapy.http.TextResponse") 它通過查看XML聲明行添加了編碼自動發現支持。見 [`TextResponse.encoding`](#scrapy.http.TextResponse.encoding "scrapy.http.TextResponse.encoding") .
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看