脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - Python - Scrapy爬虫Response子类在应用中的问题解析

Scrapy爬虫Response子类在应用中的问题解析

2023-05-17 13:17ponponon Python

这篇文章主要为大家介绍了Scrapy爬虫Response它的子类(TextResponse、HtmlResponse、XmlResponse)在应用问题解析

正文

今天用scrapy爬取壁纸的时候(url:http://pic.netbian.com/4kmein...)絮叨了一些问题,记录下来,供后世探讨,以史为鉴。**

因为网站是动态渲染的,所以选择scrapy对接selenium(scrapy抓取网页的方式和requests库相似,都是直接模拟HTTP请求,而Scrapy也不能抓取JavaScript动态渲染的网页。)

所以在Downloader Middlewares中需要得到Request并且返回一个Response,问题出在Response,通过查看官方文档发现class scrapy.http.Response(url[, status=200, headers=None, body=b'', flags=None, request=None]),随即通过from scrapy.http import Response导入Response

Scrapy爬虫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**

检查相关代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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类的定义

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@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类不可以被直接使用,需要被继承重写方法后才能使用

响应子类

?
1
2
3
4
5
6
**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发现

?
1
2
3
4
5
6
7
8
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方法已经被重写

?
1
2
3
4
5
6
7
8
9
10
@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)

所以用户想要调用Response类,必须选择调用其子类,并且重写部分方法

Scrapy爬虫入门教程十一 Request和Response(请求和响应)

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

以上就是Scrapy爬虫Response子类在应用中的问题解析的详细内容,更多关于Scrapy爬虫Response子类应用的资料请关注服务器之家其它相关文章!

原文链接:https://segmentfault.com/a/1190000018449717

延伸 · 阅读

精彩推荐
  • Pythonpytorch __init__、forward与__call__的用法小结

    pytorch __init__、forward与__call__的用法小结

    这篇文章主要介绍了pytorch __init__、forward与__call__的用法小结,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    时光碎了天11532021-09-10
  • PythonDjango框架中render_to_response()函数的使用方法

    Django框架中render_to_response()函数的使用方法

    这篇文章主要介绍了Django框架中render_to_response()函数的使用方法,注意范例中该方法的参数的使用,需要的朋友可以参考下 ...

    脚本之家13142020-07-23
  • Pythonpython目标检测SSD算法训练部分源码详解

    python目标检测SSD算法训练部分源码详解

    这篇文章主要为大家介绍了python目标检测SSD算法训练部分源码详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪...

    Bubbliiiing11212022-12-15
  • Python100 个 Python 小例子(练习题二)

    100 个 Python 小例子(练习题二)

    这篇文章主要继续上一篇文章100 个 Python 小例子(练习题一)来完成100 个 Python 小例子,本文包括字母识词、反向输出II、表转字符串、设置输出颜色、算...

    野客4982022-02-13
  • PythonPython数据结构与算法之链表,无序链表详解

    Python数据结构与算法之链表,无序链表详解

    这篇文章主要为大家详细介绍了Python数据结构与算法之链表,使用数据库,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考...

    姜学迁10502022-10-29
  • Pythonpython实现kMeans算法

    python实现kMeans算法

    这篇文章主要为大家详细介绍了python实现kMeans算法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    开贰锤24642020-12-27
  • Pythonpython决策树之C4.5算法详解

    python决策树之C4.5算法详解

    这篇文章主要为大家详细介绍了python决策树之C4.5算法的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    zhihua_oba11142020-12-27
  • PythonPython matplotlib实现折线图的绘制

    Python matplotlib实现折线图的绘制

    Matplotlib作为Python的2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形。本文将利用Matplotlib库绘制折线图,感兴趣的可以了解一...

    渴望成为寂寞胜者7762022-10-27