欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Scrapy爬蟲(chóng)Response子類(lèi)在應(yīng)用中的問(wèn)題解析

 更新時(shí)間:2023年05月16日 14:35:53   作者:ponponon  
這篇文章主要為大家介紹了Scrapy爬蟲(chóng)Response它的子類(lèi)(TextResponse、HtmlResponse、XmlResponse)在應(yīng)用問(wèn)題解析

正文

今天用scrapy爬取壁紙的時(shí)候(url:http://pic.netbian.com/4kmein...)絮叨了一些問(wèn)題,記錄下來(lái),供后世探討,以史為鑒。**

因?yàn)榫W(wǎng)站是動(dòng)態(tài)渲染的,所以選擇scrapy對(duì)接selenium(scrapy抓取網(wǎng)頁(yè)的方式和requests庫(kù)相似,都是直接模擬HTTP請(qǐng)求,而Scrapy也不能抓取JavaScript動(dòng)態(tài)渲染的網(wǎng)頁(yè)。)

所以在Downloader Middlewares中需要得到Request并且返回一個(gè)Response,問(wèn)題出在Response,通過(guò)查看官方文檔發(fā)現(xiàn)class scrapy.http.Response(url[, status=200, headers=None, body=b'', flags=None, request=None]),隨即通過(guò)from scrapy.http import Response導(dǎo)入Response

輸入scrapy crawl girl得到如下錯(cuò)誤:

*results=response.xpath('//[@id="main"]/div[3]/ul/lia/img')
raise NotSupported("Response content isn't text")
scrapy.exceptions.NotSupported: Response content isn't text**

檢查相關(guān)代碼:

# middlewares.py
from scrapy import signals
from scrapy.http import Response
from scrapy.exceptions import IgnoreRequest
import selenium
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class Pic4KgirlDownloaderMiddleware(object):
    # Not all methods need to be defined. If a method is not defined,
    # scrapy acts as if the downloader middleware does not modify the
    # passed objects.
    def process_request(self, request, spider):
        # Called for each request that goes through the downloader
        # middleware.
        # Must either:
        # - return None: continue processing this request
        # - or return a Response object
        # - or return a Request object
        # - or raise IgnoreRequest: process_exception() methods of
        #   installed downloader middleware will be called
        try:
            self.browser=selenium.webdriver.Chrome()
            self.wait=WebDriverWait(self.browser,10)
            self.browser.get(request.url)
            self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#main > div.page > a:nth-child(10)')))
            return Response(url=request.url,status=200,request=request,body=self.browser.page_source.encode('utf-8'))
        #except:
            #raise IgnoreRequest()
        finally:
            self.browser.close()

推斷問(wèn)題出在:

return Response(url=request.url,status=200,request=request,body=self.browser.page_source.encode('utf-8'))

查看Response類(lèi)的定義

@property
    def text(self):
        """For subclasses of TextResponse, this will return the body
        as text (unicode object in Python 2 and str in Python 3)
        """
        raise AttributeError("Response content isn't text")
    def css(self, *a, **kw):
        """Shortcut method implemented only by responses whose content
        is text (subclasses of TextResponse).
        """
        raise NotSupported("Response content isn't text")
    def xpath(self, *a, **kw):
        """Shortcut method implemented only by responses whose content
        is text (subclasses of TextResponse).
        """
        raise NotSupported("Response content isn't text")

說(shuō)明Response類(lèi)不可以被直接使用,需要被繼承重寫(xiě)方法后才能使用

響應(yīng)子類(lèi)

**TextResponse對(duì)象**
class scrapy.http.TextResponse(url[, encoding[, ...]])
**HtmlResponse對(duì)象**
class scrapy.http.HtmlResponse(url[, ...])
**XmlResponse對(duì)象**
class scrapy.http.XmlResponse(url [,... ] )

舉例觀察TextResponse的定義from scrapy.http import TextResponse

導(dǎo)入TextResponse發(fā)現(xiàn)

class TextResponse(Response):
    _DEFAULT_ENCODING = 'ascii'
    def __init__(self, *args, **kwargs):
        self._encoding = kwargs.pop('encoding', None)
        self._cached_benc = None
        self._cached_ubody = None
        self._cached_selector = None
        super(TextResponse, self).__init__(*args, **kwargs)

其中xpath方法已經(jīng)被重寫(xiě)

@property
    def selector(self):
        from scrapy.selector import Selector
        if self._cached_selector is None:
            self._cached_selector = Selector(self)
        return self._cached_selector
    def xpath(self, query, **kwargs):
        return self.selector.xpath(query, **kwargs)
    def css(self, query):
        return self.selector.css(query)

所以用戶想要調(diào)用Response類(lèi),必須選擇調(diào)用其子類(lèi),并且重寫(xiě)部分方法

Scrapy爬蟲(chóng)入門(mén)教程十一 Request和Response(請(qǐng)求和響應(yīng))

scrapy文檔:https://doc.scrapy.org/en/lat...

中文翻譯文檔:http://www.dbjr.com.cn/article/248161.htm

以上就是Scrapy爬蟲(chóng)Response子類(lèi)在應(yīng)用中的問(wèn)題解析的詳細(xì)內(nèi)容,更多關(guān)于Scrapy爬蟲(chóng)Response子類(lèi)應(yīng)用的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python數(shù)學(xué)建模PuLP庫(kù)線性規(guī)劃實(shí)際案例編程詳解

    Python數(shù)學(xué)建模PuLP庫(kù)線性規(guī)劃實(shí)際案例編程詳解

    本節(jié)以一個(gè)實(shí)際數(shù)學(xué)建模案例,來(lái)為大家講解PuLP求解線性規(guī)劃問(wèn)題的建模與編程。來(lái)鞏固加深大家對(duì)Python數(shù)學(xué)建模PuLP庫(kù)線性規(guī)劃的運(yùn)用理解
    2021-10-10
  • python 6.7 編寫(xiě)printTable()函數(shù)表格打印(完整代碼)

    python 6.7 編寫(xiě)printTable()函數(shù)表格打印(完整代碼)

    這篇文章主要介紹了python 6.7 編寫(xiě)一個(gè)名為printTable()的函數(shù) 表格打印,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • 用Python采集《雪中悍刀行》彈幕做成詞云實(shí)例

    用Python采集《雪中悍刀行》彈幕做成詞云實(shí)例

    大家好,本篇文章主要講的是用Python采集《雪中悍刀行》彈幕做成詞云實(shí)例,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-01-01
  • python tkinter 設(shè)置窗口大小不可縮放實(shí)例

    python tkinter 設(shè)置窗口大小不可縮放實(shí)例

    這篇文章主要介紹了python tkinter 設(shè)置窗口大小不可縮放實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03
  • Python元類(lèi)與迭代器生成器案例詳解

    Python元類(lèi)與迭代器生成器案例詳解

    這篇文章主要介紹了Python元類(lèi)與迭代器生成器案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Python 3中的yield from語(yǔ)法詳解

    Python 3中的yield from語(yǔ)法詳解

    在python 3.3里,generator新增了一個(gè)語(yǔ)法 yield from,這個(gè)yield from的作用是什么?語(yǔ)法是什么呢?下面通過(guò)這篇文章主要給大家詳細(xì)介紹了Python 3中yield from語(yǔ)法的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-01-01
  • python版大富翁源代碼分享

    python版大富翁源代碼分享

    這篇文章主要為大家詳細(xì)介紹了python版大富翁源代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • 解決tensorflow打印tensor有省略號(hào)的問(wèn)題

    解決tensorflow打印tensor有省略號(hào)的問(wèn)題

    今天小編就為大家分享一篇解決tensorflow打印tensor有省略號(hào)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-02-02
  • 詳解Python使用apscheduler定時(shí)執(zhí)行任務(wù)

    詳解Python使用apscheduler定時(shí)執(zhí)行任務(wù)

    在平常的工作中幾乎有一半的功能模塊都需要定時(shí)任務(wù)來(lái)推動(dòng),例如項(xiàng)目中有一個(gè)定時(shí)統(tǒng)計(jì)程序,定時(shí)爬出網(wǎng)站的URL程序,定時(shí)檢測(cè)釣魚(yú)網(wǎng)站的程序等等,都涉及到了關(guān)于定時(shí)任務(wù)的問(wèn)題,所以就找到了python的定時(shí)任務(wù)模塊
    2022-03-03
  • PyQt5 實(shí)現(xiàn)字體大小自適應(yīng)分辨率的方法

    PyQt5 實(shí)現(xiàn)字體大小自適應(yīng)分辨率的方法

    今天小編就為大家分享一篇PyQt5 實(shí)現(xiàn)字體大小自適應(yīng)分辨率的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-06-06

最新評(píng)論