背景:我们想要在爬虫中使用xpath、beautifulsoup、正则表达式、css选择器等来提取想要的数据,但因为Scrapy是一个比较重的框架,每次运行起来都要等待一段时间,因此要去验证我们写的提取规则是否正确,是一个比较麻烦的事情,因此Scrapy提供了一个Shell,用来方便的测试规则,当然也不仅仅局限于这一个功能。
打开cmd终端,进入到Scrapy项目所在的目录,然后进入到Scrapy框架所在的虚拟环境中,输入命令
scrapy shell [链接]
,就会进入到Scrapy的Shell环境中,在这个环境中,你可以跟在爬虫的parse
方法中一样的使用了。
D:\学习笔记\Python学习\Python_Crawler\wxapp>scrapy shell http://www.wxapp-union.com/article-5907-1.html
2020-04-27 15:21:05 [scrapy.utils.log] INFO: Scrapy 2.1.0 started (bot: wxapp)
2020-04-27 15:21:05 [scrapy.utils.log] INFO: Versions: lxml 4.5.0.0, libxml2 2.9.5, cssselect 1.1.0, parsel 1.5.2, w3lib 1.21.0, Twisted 20.3.0, Python 3.8.1 (tags
/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)], pyOpenSSL 19.1.0 (OpenSSL 1.1.1g 21 Apr 2020), cryptography 2.9.2, Platform Windows-10-10.0.1
4393-SP0
2020-04-27 15:21:05 [scrapy.utils.log] DEBUG: Using reactor: twisted.internet.selectreactor.SelectReactor
2020-04-27 15:21:05 [scrapy.crawler] INFO: Overridden settings:
{'BOT_NAME': 'wxapp',
'DOWNLOAD_DELAY': 1,
'DUPEFILTER_CLASS': 'scrapy.dupefilters.BaseDupeFilter',
'LOGSTATS_INTERVAL': 0,
'NEWSPIDER_MODULE': 'wxapp.spiders',
'SPIDER_MODULES': ['wxapp.spiders']}
2020-04-27 15:21:05 [scrapy.extensions.telnet] INFO: Telnet Password: 7fff3f5089f7c6fb
2020-04-27 15:21:05 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
'scrapy.extensions.telnet.TelnetConsole']
2020-04-27 15:21:06 [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
'scrapy.downloadermiddlewares.retry.RetryMiddleware',
'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
'scrapy.downloadermiddlewares.stats.DownloaderStats']
2020-04-27 15:21:06 [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
'scrapy.spidermiddlewares.referer.RefererMiddleware',
'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
'scrapy.spidermiddlewares.depth.DepthMiddleware']
2020-04-27 15:21:06 [scrapy.middleware] INFO: Enabled item pipelines:
['wxapp.pipelines.WxappPipeline']
2020-04-27 15:21:06 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023
2020-04-27 15:21:06 [scrapy.core.engine] INFO: Spider opened
2020-04-27 15:21:06 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://www.wxapp-union.com/article-5907-1.html> (referer: None)
2020-04-27 15:21:07 [asyncio] DEBUG: Using proactor: IocpProactor
[s] Available Scrapy objects:
[s] scrapy scrapy module (contains scrapy.Request, scrapy.Selector, etc)
[s] crawler <scrapy.crawler.Crawler object at 0x00000237DC08EB50>
[s] item {}
[s] request <GET http://www.wxapp-union.com/article-5907-1.html>
[s] response <200 http://www.wxapp-union.com/article-5907-1.html>
[s] settings <scrapy.settings.Settings object at 0x00000237DC08E220>
[s] spider <WxappspiderSpider 'wxappSpider' at 0x237dd0e2bb0>
[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
2020-04-27 15:21:07 [asyncio] DEBUG: Using proactor: IocpProactor
In [1]: response.xpath(r"//h1[@class='ph']/text()").get()
Out[1]: '开发一个时间小程序 '
In [2]: from bs4 import BeautifulSoup
In [3]: soup = BeautifulSoup(response.text, 'lxml')
In [4]: soup.find('h1', attrs = {"class":"ph"})
Out[4]: <h1 class="ph">开发一个时间小程序 </h1>
1)Scrapy Shell可以方便我们做一些数据提取的测试代码;
2)如果想要执行Scrapy命令,那么毫无疑问,肯定是要先进入到Scrapy所在的环境中;
3)如果想要读取某个项目的配置信息,那么应该先进入到这个项目中,再执行scrapy shell
命令。