最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Scrapy爬蟲Response子類在應用中的問題解析

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

正文

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

因為網(wǎng)站是動態(tài)渲染的,所以選擇scrapy對接selenium(scrapy抓取網(wǎng)頁的方式和requests庫相似,都是直接模擬HTTP請求,而Scrapy也不能抓取JavaScript動態(tài)渲染的網(wǎng)頁。)

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

輸入scrapy crawl girl得到如下錯誤:

*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()

推斷問題出在:

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

查看Response類的定義

@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")

說明Response類不可以被直接使用,需要被繼承重寫方法后才能使用

響應子類

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

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

導入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)被重寫

@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類,必須選擇調(diào)用其子類,并且重寫部分方法

Scrapy爬蟲入門教程十一 Request和Response(請求和響應)

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

中文翻譯文檔:http://m.fzitv.net/article/248161.htm

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

相關(guān)文章

  • Python采集王者最低戰(zhàn)力信息實戰(zhàn)示例

    Python采集王者最低戰(zhàn)力信息實戰(zhàn)示例

    這篇文章主要為大家介紹了Python采集王者最低戰(zhàn)力信息實戰(zhàn)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04
  • miniconda3介紹、安裝以及使用教程

    miniconda3介紹、安裝以及使用教程

    Miniconda是一款小巧的python環(huán)境管理工具,安裝包大約只有50M多點,其安裝程序中包含conda軟件包管理器和Python,下面這篇文章主要給大家介紹了關(guān)于miniconda3介紹、安裝以及使用的相關(guān)資料,需要的朋友可以參考下
    2023-02-02
  • python 實現(xiàn)數(shù)組list 添加、修改、刪除的方法

    python 實現(xiàn)數(shù)組list 添加、修改、刪除的方法

    下面小編就為大家分享一篇python 實現(xiàn)數(shù)組list 添加、修改、刪除的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • 人工智能Text Generation文本生成原理示例詳解

    人工智能Text Generation文本生成原理示例詳解

    這篇文章主要為大家介紹了Text Generation文本生成原理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01
  • Python實現(xiàn)統(tǒng)計代碼行的方法分析

    Python實現(xiàn)統(tǒng)計代碼行的方法分析

    這篇文章主要介紹了Python實現(xiàn)統(tǒng)計代碼行的方法,結(jié)合實例形式分析了Python針對代碼行數(shù)的計算實現(xiàn)步驟與操作技巧,需要的朋友可以參考下
    2017-07-07
  • 基于python調(diào)用psutil模塊過程解析

    基于python調(diào)用psutil模塊過程解析

    這篇文章主要介紹了基于python調(diào)用psutils模塊過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • Python多進程原理與用法分析

    Python多進程原理與用法分析

    這篇文章主要介紹了Python多進程原理與用法,結(jié)合實例形式分析了Python多進程原理、開啟使用進程、進程隊列、進程池等相關(guān)概念與使用方法,需要的朋友可以參考下
    2018-08-08
  • python實戰(zhàn)之百度智能云使人像動漫化

    python實戰(zhàn)之百度智能云使人像動漫化

    這篇文章主要介紹了python實戰(zhàn)之百度智能云使人像動漫化,文中有非常詳細的代碼示例,對正在學習python的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-04-04
  • 人工智能學習PyTorch實現(xiàn)CNN卷積層及nn.Module類示例分析

    人工智能學習PyTorch實現(xiàn)CNN卷積層及nn.Module類示例分析

    這篇文章主要為大家介紹了人工智能學習PyTorch實現(xiàn)CNN卷積層及nn.Module類示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-11-11
  • 詳解Python遍歷字典的鍵和值

    詳解Python遍歷字典的鍵和值

    這篇文章主要通過一些簡單的示例為大家介紹一下Python中遍歷字典的鍵和值的相關(guān)知識,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下
    2023-03-03

最新評論

农安县| 新安县| 仁化县| 枣阳市| 阜南县| 乾安县| 榆林市| 黔西县| 霍州市| 沿河| 登封市| 高陵县| 通河县| 图片| 偏关县| 林甸县| 镇安县| 米泉市| 方正县| 潞城市| 延庆县| 探索| 横山县| 湘潭市| 新丰县| 延庆县| 甘肃省| 霍邱县| 乐业县| 芜湖县| 泽库县| 昔阳县| 安乡县| 拉萨市| 霍山县| 十堰市| 榆林市| 米泉市| 合阳县| 阿图什市| 镇宁|