以單個頁面爲例,如:http://blog.jobbole.com/110287/css
咱們能夠提取標題、日期、多少個評論、正文內容等html
(1) xpath使用路徑表達式在xml和html中進行導航node
(2) xpath包含標準函數庫shell
(3) xpath是一個w3c標準數組
(1) 父節點cookie
(2) 子節點scrapy
(3) 同胞節點ide
(4) 先輩節點函數
(5) 後代節點post
1. 將starts_urls修改成http://blog.jobbole.com/110287/
2. def parse(self, response)方法中的response自帶有xpath方法
結果爲://*[@id="post-110287"]/div[1]/h1
在parse方法裏面進行解析:
#業務邏輯 def parse(self, response): title_selector = response.xpath('//*[@id="post-110287"]/div[1]/h1') pass
斷點查看返回的title_selector的類型是SelectorList,裏面存放的是一個selector,爲何不直接返回一個node類型,這是由於咱們獲取的h1下面可能還有不少層,咱們能夠進一步的作xpath篩選,直接返回node的話就沒法作xpath篩選,因此scrapy作了進一步的封裝,能夠讓咱們進一步進行xpath篩選。
咱們能夠看到data裏面的值是一個h1標籤,咱們能夠直接調用一個text()函數獲取裏面的值,以下
title_selector = response.xpath('//*[@id="post-110287"]/div[1]/h1/text()')
此時在看data裏面值就是咱們須要的內容了
在scarpy裏面進行調試是比較慢的,scrapy提供了一種shell模式,能夠在裏面對某個URL進行調試,以下在cmd裏面
(scrapyenv) E:\Python\Envs\EnterpriseSpider>scrapy shell http://blog.jobbole.com/110287/
(scrapyenv) E:\Python\Envs\EnterpriseSpider>scrapy shell http://blog.jobbole.com/110287/ 2018-11-05 10:14:15 [scrapy.utils.log] INFO: Scrapy 1.5.1 started (bot: EnterpriseSpider) 2018-11-05 10:14:15 [scrapy.utils.log] INFO: Versions: lxml 4.2.5.0, libxml2 2.9.5, cssselect 1.0.3, parsel 1.5.1, w3lib 1.19.0, Twisted 18.9.0, Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)], pyOpenSSL 18.0.0 (OpenSSL 1.1.0i 14 Aug 2018), cryptography 2.3.1, Platform Windows-10-10.0.17134-SP0 2018-11-05 10:14:15 [scrapy.crawler] INFO: Overridden settings: {'BOT_NAME': 'EnterpriseSpider', 'DUPEFILTER_CLASS': 'scrapy.dupefilters.BaseDupeFilter', 'LOGSTATS_INTERVAL': 0, 'NEWSPIDER_MODULE': 'EnterpriseSpider.spiders', 'SPIDER_MODULES': ['EnterpriseSpider.spiders']} 2018-11-05 10:14:15 [scrapy.middleware] INFO: Enabled extensions: ['scrapy.extensions.corestats.CoreStats', 'scrapy.extensions.telnet.TelnetConsole'] 2018-11-05 10:14:15 [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'] 2018-11-05 10:14:15 [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'] 2018-11-05 10:14:15 [scrapy.middleware] INFO: Enabled item pipelines: [] 2018-11-05 10:14:15 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023 2018-11-05 10:14:15 [scrapy.core.engine] INFO: Spider opened 2018-11-05 10:14:15 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://blog.jobbole.com/110287/> (referer: None) [s] Available Scrapy objects: [s] scrapy scrapy module (contains scrapy.Request, scrapy.Selector, etc) [s] crawler <scrapy.crawler.Crawler object at 0x000002C00C180080> [s] item {} [s] request <GET http://blog.jobbole.com/110287/> [s] response <200 http://blog.jobbole.com/110287/> [s] settings <scrapy.settings.Settings object at 0x000002C00E7DD898> [s] spider <JobboleSpider 'jobbole' at 0x2c00ea8b160> [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 >>>
使用以下
獲取data裏面值能夠用Selector的extract方法,返回的是一個數組
在獲取數組裏面的第一個值就能夠獲取咱們須要的內容