笨鸟编程-零基础入门Pyhton教程

 找回密码
 立即注册

Scrapy shell

发布者: 笨鸟自学网



使用外壳

scrappyshell只是一个普通的python控制台(或者 IPython 控制台,如果你有它的话),它提供一些额外的快捷功能,以方便。

可用快捷方式

  • shelp() -打印有关可用对象和快捷方式列表的帮助

  • fetch(url[, redirect=True]) - fetch a new response from the given URL and update all related objects accordingly. You can optionally ask for HTTP 3xx redirections to not be followed by passing redirect=False

  • fetch(request) -从给定的请求中获取新的响应,并相应地更新所有相关对象。

  • view(response) -在本地Web浏览器中打开给定的响应以进行检查。这将增加一个 <base> tag 到响应主体,以便外部链接(如图像和样式表)正确显示。但是请注意,这将在您的计算机中创建一个临时文件,该文件不会自动删除。

可用的 Scrapy 对象

Scrapy shell自动从下载的页面创建一些方便的对象,如 Response 对象和 Selector 对象(用于HTML和XML内容)。

这些对象是:

  • crawler -电流 Crawler 对象。

  • spider -已知处理URL的爬行器,或 Spider 如果没有找到当前URL的爬行器,则返回

  • request -a Request 上一次获取的页的。您可以使用以下命令修改此请求 replace() 方法获取新请求(无需离开shell)。 fetch 捷径。

  • response -A Response 包含上次提取的页的对象

  • settings - the current Scrapy settings

Shell会话示例

下面是一个典型的shell会话的示例,我们从https://scray.org页面,然后继续刮https://old.reddit.com/第页。最后,我们修改(Reddit)请求方法,以发布并重新获取它,得到一个错误。我们通过键入Ctrl-D(在Unix系统中)或在Windows中键入Ctrl-Z来结束会话。

请记住,在这里提取的数据在您尝试时可能不相同,因为这些页面不是静态的,在您测试时可能已经更改了。这个例子的唯一目的是让您熟悉下脚料外壳的工作原理。

首先,我们发射炮弹:

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

注解

当从命令行运行Scrapy shell时,记住总是用引号括住url,否则url包含参数(即 & 字符)不起作用。

在Windows上,使用双引号:

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

然后,shell获取URL(使用scrapy下载器)并打印可用对象和有用快捷方式的列表(您会注意到这些行都以 [s] 前缀):

[s] Available Scrapy objects:
[s]   scrapy     scrapy module (contains scrapy.Request, scrapy.Selector, etc)
[s]   crawler    <scrapy.crawler.Crawler object at 0x7f07395dd690>
[s]   item       {}
[s]   request    <GET https://scrapy.org>
[s]   response   <200 https://scrapy.org/>
[s]   settings   <scrapy.settings.Settings object at 0x7f07395dd710>
[s]   spider     <DefaultSpider 'default' at 0x7f0735891690>
[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

>>>

之后,我们可以开始玩对象:

>>> response.xpath('//title/text()').get()
'Scrapy | A Fast and Powerful Scraping and Web Crawling Framework'
>>> fetch("https://old.reddit.com/")
>>> response.xpath('//title/text()').get()
'reddit: the front page of the internet'
>>> request = request.replace(method="POST")
>>> fetch(request)
>>> response.status
404
>>> 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']} 

上一篇:项目加载器下一篇:项目管道

Archiver|手机版|笨鸟自学网 ( 粤ICP备20019910号 )

GMT+8, 2024-9-8 11:32 , Processed in 0.192134 second(s), 17 queries .

© 2001-2020

返回顶部