9.scrapy pycharm调试小技巧,请求一次,下次直接调试,不必每次都启动整个爬虫,重新请求一整遍
pycharm调试技巧:调试时,请求一次,下次直接调试,不必每次都启动整个爬虫,重新请求一整遍
【用法】cmd命令运行:scrapy shell 网址
第一步,cmd进行一次请求:
scrapy shell http://blog.jobbole.com/114633/
返回详情:
C:\Users\Administrator>scrapy shell http://blog.jobbole.com/114633/ 2019-05-06 14:47:09 [scrapy.utils.log] INFO: Scrapy 1.6.0 started (bot: scrapybo t) 2019-05-06 14:47:09 [scrapy.utils.log] INFO: Versions: lxml 4.3.3.0, libxml2 2.9 .5, cssselect 1.0.3, parsel 1.5.1, w3lib 1.20.0, Twisted 19.2.0, Python 3.7.3 (v 3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)], pyOpenSSL 19.0.0 (OpenSSL 1.1.1b 26 Feb 2019), cryptography 2.6.1, Platform Windows-7-6.1 .7601-SP1 2019-05-06 14:47:09 [scrapy.crawler] INFO: Overridden settings: {'DUPEFILTER_CLA SS': 'scrapy.dupefilters.BaseDupeFilter', 'LOGSTATS_INTERVAL': 0} 2019-05-06 14:47:09 [scrapy.extensions.telnet] INFO: Telnet Password: ad4920d938 db236a 2019-05-06 14:47:09 [scrapy.middleware] INFO: Enabled extensions: ['scrapy.extensions.corestats.CoreStats', 'scrapy.extensions.telnet.TelnetConsole'] 2019-05-06 14:47:10 [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'] 2019-05-06 14:47:10 [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'] 2019-05-06 14:47:10 [scrapy.middleware] INFO: Enabled item pipelines: [] 2019-05-06 14:47:10 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023 2019-05-06 14:47:10 [scrapy.core.engine] INFO: Spider opened 2019-05-06 14:47:11 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://blog.j obbole.com/114633/> (referer: None) [s] Available Scrapy objects: [s] scrapy scrapy module (contains scrapy.Request, scrapy.Selector, etc) [s] crawler <scrapy.crawler.Crawler object at 0x0000000004A07C50> [s] item {} [s] request <GET http://blog.jobbole.com/114633/> [s] response <200 http://blog.jobbole.com/114633/> [s] settings <scrapy.settings.Settings object at 0x0000000004A07A90> [s] spider <DefaultSpider 'default' at 0x4db6e48> [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
第二步,进行调试(好处,只要请求次,其它调试都不用再次请求):
#用xpath >>> title=response.xpath("//div[@class='entry-header']/h1/text()") #title是一个对象 >>> print(title) [<Selector xpath="//div[@class='entry-header']/h1/text()" data='“三次握手,四次挥手”你真的懂吗?'>] #返回列表extract()方法 >>> title.extract() ['“三次握手,四次挥手”你真的懂吗?'] #返回列表值 >>> title.extract()[0] '“三次握手,四次挥手”你真的懂吗?'