zoukankan      html  css  js  c++  java
  • 二.Pyhon_scrapy终端(scrapy shell)学习笔记

    Scrapy shell

    Scrapy shell是一个交互式shell,您可以非常快速地尝试调试您的抓取代码,而无需运行蜘蛛。它用于测试数据提取代码,但您实际上可以使用它来测试任何类型的代码,因为它也是常规的Python shell。

    配置

    官方原文:如果安装了IPython,Scrapy shell将使用它(而不是标准的Python控制台)。IPython的控制台功能更强大,并提供智能自动完成和彩色输出,等等。

    我们强烈建议您安装IPython,特别是如果您正在使用Unix系统(IPython擅长)。有关 详细信息,请参阅IPython安装指南

    Scrapy也支持bpython,并且会尝试在IPython 不可用的地方使用它

    调用的话,可以进入你文件中的scrapy.cfg中设置,添加,例如ipython:

    可以在笔记一的E:pythoncode中设置:

    [settings]
    shell = ipython

    启动

    进入命令行
    scrapy shell <url>

    scrapy也可以抓取本地文件:

    scrapy shell X:///XXX/XXX/XXX/XXX.html

    使用

    Scrapy shell只是一个常规的Python控制台(如果有的话,它可以是IPython控制台),它提供了一些额外的快捷功能以方便使用。

    Available Shortcuts(可用的命令?)

    shelp()

    fetch(url[, redirect=True]) 

    fetch(request) 

    view(response) 

    可用的Scrapy对象

    Scrapy shell自动从下载的页面创建一些方便的对象,如Response对象和 Selector对象

    crawler- 当前Crawler对象。
    spider- 已知处理URL的Spider,或者Spider当前URL没有找到蜘蛛时的 对象
    request- Request最后一个获取页面的对象。您可以replace() 使用fetch 快捷方式使用或获取新请求(不离开shell)来修改此请求。
    response- Response包含最后一个提取页面的对象
    settings- 目前的Scrapy设置

    shell会话的例子

    首先,进入E:pythoncode,然后启动shell:

    scrapy shell "https://www.baidu.com" --nolog

    可以看到使用的一些命令:

    [s] Available Scrapy objects:
    [s] scrapy scrapy module (contains scrapy.Request, scrapy.Selector, etc)
    [s] crawler <scrapy.crawler.Crawler object at 0x0000000000B27390>
    [s] item {}
    [s] request <GET https://www.baidu.com>
    [s] settings <scrapy.settings.Settings object at 0x0000000004BA03C8>
    [s] Useful shortcuts:
    [s] fetch(url[, redirect=True]) Fetch URL and update local objects (by default
    , redirects are followed)
    [s] fetch(req) Fetch a scrapy.Request and update local object
    s
    [s] shelp() Shell help (print this help)
    [s] view(response) View response in a browser

    接着我们输入:
    response.css("div.celltop a b::text").extract_first() 'Information'
    fetch("http://www.guoxuedashi.com/")
    注:记得url要加前缀(http://或者https://
    注:如果前面scrapy shell的时候没有加--nolog,会显示
    注:DEBUG: Crawled (200)XXXXXXXXXXXXXXXXXX

    response.css("a[target=_blank]::text").extract_first()
    '四库全书'

    request = request.replace(method="POST")

    fetch(request)

    注:"POST","GET","PUT","HEAD"等等都是HTTP请求方法(一般是用GET,这里用POST是想举个例子)

    response.status
    200

    注:200是网页响应代码

    from pprint import pprint

    pprint(response.headers)

    注:ppint是美观的print

    从爬虫中调用shell

    有时您想要检查蜘蛛的某个特定点正在处理的响应,如果只是为了检查您期望的响应是否到达那里。

    这可以通过使用该scrapy.shell.inspect_response功能来实现。

    在E:pythoncodemyprojectspiders创建

    import scrapy
    
    
    class MySpider(scrapy.Spider):
        name = "scrapy_sh"
        start_urls = [
            "http://example.com",
            "http://example.org",
            "http://example.net",
        ]
    
        def parse(self, response):        
            if ".org" in response.url:
                from scrapy.shell import inspect_response
                inspect_response(response, self)
           
    注:shell就出来了~
    response.url
    'http://example.org'

    response.css("p::text").extract()
    ["This domain is established to be used for illustrative examples in doc..........."]

    view(response)
    True

    注:Ctrl+Z或者Ctrl+D可以退出

     附上源头活水:https://docs.scrapy.org/en/latest/topics/shell.html

  • 相关阅读:
    关于前端基础框架的思考和尝试
    通过当前IP获取当前网卡的MAC地址
    shell及脚本2——shell 环境及命令
    shell及脚本1——变量
    linux显示git commit id,同时解决insmod模块时版本不一致导致无法加载问题
    大于16MB的QSPI存放程序引起的ZYNQ重启风险
    insmod模块的几种常见错误
    shell及脚本3——正则表达式
    修改/etc/profile和/etc/environment导致图形界面无法登陆的问题
    Sql 2008的merge关键字
  • 原文地址:https://www.cnblogs.com/oxxxo/p/9787989.html
Copyright © 2011-2022 走看看