资讯专栏INFORMATION COLUMN

Scrapy中的Reponse和它的子类(TextResponse、HtmlResponse、Xml

Ashin / 2124人阅读

摘要:今天用爬取壁纸的时候絮叨了一些问题,记录下来,供后世探讨,以史为鉴。因为网站是动态渲染的,所以选择对接抓取网页的方式和库相似,都是直接模拟请求,而也不能抓取动态渲染的网页。

今天用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 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**
检查相关代码:

# 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
发现

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方法已经被重写

@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...
中文翻译文档:https://blog.csdn.net/Inke88/...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/43318.html

相关文章

  • Python网页信息采集:使用PhantomJS采集淘宝天猫商品内容

    摘要:,引言最近一直在看爬虫框架,并尝试使用框架写一个可以实现网页信息采集的简单的小程序。本文主要介绍如何使用结合采集天猫商品内容,文中自定义了一个,用来采集需要加载的动态网页内容。 showImg(https://segmentfault.com/img/bVyMnP); 1,引言 最近一直在看Scrapy 爬虫框架,并尝试使用Scrapy框架写一个可以实现网页信息采集的简单的小程序。尝试...

    z2xy 评论0 收藏0
  • Scrapy+Chromium+代理+selenium

    摘要:通常的解决办法是通过抓包,然后查看信息,接着捕获返回的消息。为了减少因为安装环境所带来的烦恼。代理因为我们已经用替换了。我们需要直接用来处理代理问题。根据上面这段代码,我们也不难猜出解决代理的方法了。 上周说到scrapy的基本入门。这周来写写其中遇到的代理和js渲染的坑。 js渲染 js是爬虫中毕竟麻烦处理的一块。通常的解决办法是通过抓包,然后查看request信息,接着捕获ajax...

    Pocher 评论0 收藏0
  • scrapy 进阶使用

    摘要:下载器负责获取页面,然后将它们交给引擎来处理。内置了一些下载器中间件,这些中间件将在后面介绍。下载器中间件下载器中间件可以在引擎和爬虫之间操纵请求和响应对象。爬虫中间件与下载器中间件类似,启用爬虫中间件需要一个字典来配置。 前段时间我写了一篇《scrapy快速入门》,简单介绍了一点scrapy的知识。最近我的搬瓦工让墙了,而且我又学了一点mongodb的知识,所以这次就来介绍一些scr...

    The question 评论0 收藏0
  • Scrapy 爬取七麦 app数据排行榜

    摘要:目录前言创建项目创建创建解析付费榜运行爬取初始列表调用脚本获取详情前言熟悉之后,本篇文章带大家爬取七麦数据的付费应用排行榜前名应用。根据传入的正则表达式对数据进行提取,返回字符串列表。 目录 前言 创建项目 创建Item 创建Spider 解析付费榜 运行爬取初始app列表 Selenium调用JS脚本 获取app详情 前言 熟悉Scrapy之后,本篇文章带大家爬取七麦数据(h...

    kk_miles 评论0 收藏0

发表评论

0条评论

Ashin

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<