Scrapy基本概念——Scrapy shell
创始人
2024-02-23 12:38:36
0

Scrapy shell是一个交互式shell,可以在不运行Spider的情况下,测试和调试自己的数据提取代码。事实上,Scrapy shell可以测试任何类型的代码,因为它本就是一个常规的Python shell。

一、Scrapy shell的使用

1、启动Scrapy shell

scrapy shell 'https://scrapy.org' --nolog

2、使用实例和函数调试

1、使用response实例提取数据

>>> response.xpath('//title/text()').get()
'Scrapy | A Fast and Powerful Scraping and Web Crawling Framework'

2、使用fetch()函数获取响应

>>> fetch("https://old.reddit.com/")

3、使用response实例提取数据

>>> response.xpath('//title/text()').get()
'reddit: the front page of the internet'

4、使用request实例修改请求方式

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

5、使用fetch()函数获取响应

>>> fetch(request)

6、使用response实例查看响应状态

>>> response.status
404

7、使用response实例打印响应头信息

>>> from pprint import pprint
>>> pprint(response.headers)
{'Accept-Ranges': ['bytes'],'Cache-Control': ['max-age=0, must-revalidate'],'Content-Type': ['text/html; charset=UTF-8'],'Date': ['Thu, 08 Dec 2016 16:21:19 GMT'],'Server': ['snooserv'],'Set-Cookie': ['loid=KqNLou0V9SKMX4qb4n; Domain=reddit.com; Max-Age=63071999; Path=/; expires=Sat, 08-Dec-2018 16:21:19 GMT; secure', 'loidcreated=2016-12-08T16%3A21%3A19.445Z; Domain=reddit.com; Max-Age=63071999; Path=/; expires=Sat, 08-Dec-2018 16:21:19 GMT; secure', 'loid=vi0ZVe4NkxNWdlH7r7; Domain=reddit.com; Max-Age=63071999; Path=/; expires=Sat, 08-Dec-2018 16:21:19 GMT; secure','loidcreated=2016-12-08T16%3A21%3A19.459Z; Domain=reddit.com; Max-Age=63071999; Path=/; expires=Sat, 08-Dec-2018 16:21:19 GMT; secure'],'Vary': ['accept-encoding'],'Via': ['1.1 varnish'],'X-Cache': ['MISS'],'X-Cache-Hits': ['0'],'X-Content-Type-Options': ['nosniff'],'X-Frame-Options': ['SAMEORIGIN'],'X-Moose': ['majestic'],'X-Served-By': ['cache-cdg8730-CDG'],'X-Timer': ['S1481214079.394283,VS0,VE159'],'X-Ua-Compatible': ['IE=edge'],'X-Xss-Protection': ['1; mode=block']}

二、Scrapy shell可用的实例和函数

[s] Available Scrapy objects:
[s]   scrapy     scrapy module (contains scrapy.Request, scrapy.Selector, etc)
[s]   crawler    
[s]   item       {}
[s]   request    
[s]   response   <200 https://scrapy.org/>
[s]   settings   
[s]   spider     
[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 objects
[s]   shelp()           Shell help (print this help)
[s]   view(response)    View response in a browser

三、在Spider中调用shell来检查响应

Spider代码中可以通过scrapy.shell.inspect_response调用shell

import scrapy
class MySpider(scrapy.Spider):name = "myspider"start_urls = ["http://example.com","http://example.org","http://example.net",]def parse(self, response):# We want to inspect one specific response.if ".org" in response.url:from scrapy.shell import inspect_responseinspect_response(response, self)# Rest of parsing code.

在执行Spider代码时,会自动启动Scrapy shell,如

2014-01-23 17:48:31-0400 [scrapy.core.engine] DEBUG: Crawled (200)  (referer: None)
2014-01-23 17:48:31-0400 [scrapy.core.engine] DEBUG: Crawled (200)  (referer: None)
[s] Available Scrapy objects:
[s]   crawler    
...>>> response.url
'http://example.org'

相关内容

热门资讯

鱼全翻着白肚皮,水还变黑了!养... 图片来源:摄图网 本文为《方圆》杂志原创稿件 这天,养鱼户陆大军发现 鱼塘里的鱼竟然全部翻着白肚皮,...
问法预告|遇到网络纠纷不知如何... 如今,互联网已经成为人们生活工作的重要场所,随着应用场景的增多,与互联网有关的纠纷也呈现上升趋势。从...
电商平台律师推荐:上海正策(杭... 在电商行业蓬勃发展的今天,各类法律问题也随之而来,选择一位靠谱的电商平台律师至关重要。那么,电商平台...
东城区“集贤人才”大会举办 发... 人民网北京12月28日电 (记者池梦蕊)12月27日,以“集贤东城·‘才’创未来”为主题的东城区“集...
闪评 | 日本内阁连曝政治资金... 当地时间12月26日,日本总务大臣林芳正公开道歉,原因在于其竞选团队向山口县选举管理委员会提交的去年...
不靠美政府建高铁了!加州撤回起... 央视记者当地时间12月27日获悉,美国加利福尼亚州已正式撤回此前针对美联邦政府的诉讼,不再挑战联邦政...
专家:存量PPP项目合同纠纷解... 记者 杜涛 “PPP(政府与社会资本合作)项目纠纷发生时,首要问题不是争论谁对谁错,而是先明确把争议...
每周股票复盘:恒兴新材(603... 截至2025年12月26日收盘,恒兴新材(603276)报收于16.64元,较上周的16.58元上涨...
知名职务侵占罪律师哪家好?周乃... 在涉及职务侵占罪这类复杂的刑事案件时,选择一位靠谱、专业且性价比高的律师至关重要。那么,市场上知名职...
政策“加减法” 中国多城楼市成... 在房地产市场调整转型的过渡期,中国多个核心城市从供需两端发力,房地产市场交易走稳。 数据显示,今年前...