教程 > scrapy 教程 > 阅读:19

scrapy shell——迹忆客-ag捕鱼王app官网

scrapy shell 可以使用无错误代码来抓取数据,而不需要使用 spider。 scrapy shell 的主要目的是测试提取的代码、xpath 或 css 表达式。 它还有助于指定我们从中抓取数据的网页。

配置shell

可以通过安装 ipython(用于交互式计算)控制台来配置 shell,这是一个强大的交互式 shell,可以提供自动完成、彩色输出等功能。

如果我们在 unix 平台上工作,那么最好安装 ipython。 如果无法访问 ipython,我们也可以使用 bpython

我们可以通过设置名为 scrapy_python_shell 的环境变量或通过如下定义 scrapy.cfg 文件来配置 shell。

[settings]
shell = bpython

启动shell

可以使用以下命令启动 scrapy shell

$ scrapy shell 

url 指定需要抓取数据的 url。


使用shell

shell 提供了一些额外的快捷方式和 scrapy 对象,如下表所述

可用的快捷方式

shell 在项目中提供了以下可用的快捷方式

序号 快捷方式 描述
1 shelp() 它通过帮助选项提供可用的对象和快捷方式。
2 fetch(request_or_url) 它收集来自请求或 url 的响应,相关对象将得到正确更新。
3 view(response) 我们可以在本地浏览器中查看给定请求的响应以进行观察并正确显示外部链接,它将基本标记附加到响应主体。

可用的 scrapy 对象

shell在项目中提供了以下可用的scrapy对象

序号 对象 描述
1 crawler 它指定当前的爬虫对象。
2 spider 如果当前 url 没有蜘蛛,那么它将通过定义新的蜘蛛来处理 url 或蜘蛛对象。
3 request 它指定最后收集的页面的请求对象。
4 response 它指定最后收集的页面的响应对象。
5 settings 它提供了当前的 scrapy 设置。

shell 会话示例

让我们尝试抓取 scrapy.org 站点,然后开始按照描述从 reddit.com 抓取数据。

在继续之前,首先我们将启动 shell,如以下命令所示

$ scrapy shell 'http://scrapy.org' --nolog

scrapy 将在使用上述 url 时显示可用对象

[s] available scrapy objects:
[s]   crawler    
[s]   item       {}
[s]   request    
[s]   response   <200 http://scrapy.org >
[s]   settings   
[s]   spider     
[s] useful shortcuts:
[s]   shelp()           provides available objects and shortcuts with help option
[s]   fetch(req_or_url) collects the response from the request or url and associated 
objects will get update
[s]   view(response)    view the response for the given request

接下来开始对象的工作,如下所示

>> response.xpath('//title/text()').extract_first() 
u'scrapy | a fast and powerful scraping and web crawling framework'  
>> fetch("http://reddit.com") 
[s] available scrapy objects: 
[s]   crawler     
[s]   item       {} 
[s]   request     
[s]   response   <200 https://www.reddit.com/> 
[s]   settings    
[s]   spider      
[s] useful shortcuts: 
[s]   shelp()           shell help (print this help) 
[s]   fetch(req_or_url) fetch request (or url) and update local objects 
[s]   view(response)    view response in a browser  
>> response.xpath('//title/text()').extract() 
[u'reddit: the front page of the internet']  
>> request = request.replace(method="post")  
>> fetch(request) 
[s] available scrapy objects: 
[s]   crawler     
... 

从蜘蛛调用 shell 来检查响应

仅当我们希望获得该响应时,我们才可以检查从蜘蛛处理的响应。

例如

import scrapy 
class spiderdemo(scrapy.spider): 
   name = "spiderdemo" 
   start_urls = [ 
      "http://mysite.com", 
      "http://mysite1.org", 
      "http://mysite2.net", 
   ]  
   
   def parse(self, response): 
      # you can inspect one specific response 
      if ".net" in response.url: 
         from scrapy.shell import inspect_response 
         inspect_response(response, self)

如上面的代码所示,我们可以使用以下函数调用蜘蛛的 shell 来检查响应

scrapy.shell.inspect_response

现在运行蜘蛛,你会得到以下结果

2022-02-08 18:15:20-0400 [scrapy] debug: crawled (200)  (referer: none) 
2022-02-08 18:15:20-0400 [scrapy] debug: crawled (200)  (referer: none) 
2022-02-08 18:15:20-0400 [scrapy] debug: crawled (200)  (referer: none) 
[s] available scrapy objects: 
[s]   crawler     
...  
>> response.url 
'http://mysite2.org' 

我们可以使用以下代码检查提取的代码是否正常工作

>> response.xpath('//div[@class = "val"]')

它将输出显示为

[]

上面的行只显示了一个空白输出。 现在您可以调用 shell 来检查响应,如下所示

>> view(response)

响应结果如下

true

查看笔记

扫码一下
查看教程更方便
网站地图