這篇文章主要介紹CSS選擇器如何實(shí)現(xiàn)字段,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
我們提供的服務(wù)有:網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、文水ssl等。為千余家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的文水網(wǎng)站制作公司
根據(jù)上面所學(xué)的CSS基礎(chǔ)語(yǔ)法知識(shí),現(xiàn)在來(lái)實(shí)現(xiàn)字段的解析。首先還是解析標(biāo)題。打開(kāi)網(wǎng)頁(yè)開(kāi)發(fā)者工具,找到標(biāo)題所對(duì)應(yīng)的源代碼。
發(fā)現(xiàn)是在div class="entry-header"
下面的h2節(jié)點(diǎn)中,于是打開(kāi)scrapy shell 進(jìn)行調(diào)試
但是我不想要
注意的是兩個(gè)冒號(hào)。使用CSS選擇器真的很方便。同理我用CSS實(shí)現(xiàn)字段解析。代碼如下
# -*- coding: utf-8 -*- import scrapy import re class JobboleSpider(scrapy.Spider): name = 'jobbole' allowed_domains = ['blog.jobbole.com'] start_urls = ['http://blog.jobbole.com/113549/'] def parse(self, response): # title = response.xpath('//div[@class = "entry-header"]/h2/text()').extract()[0] # create_date = response.xpath("http://p[@class = 'entry-meta-hide-on-mobile']/text()").extract()[0].strip().replace("·","").strip() # praise_numbers = response.xpath("http://span[contains(@class,'vote-post-up')]/h20/text()").extract()[0] # fav_nums = response.xpath("http://span[contains(@class,'bookmark-btn')]/text()").extract()[0] # match_re = re.match(".*?(\d+).*",fav_nums) # if match_re: # fav_nums = match_re.group(1) # comment_nums = response.xpath("http://a[@href='#article-comment']/span").extract()[0] # match_re = re.match(".*?(\d+).*", comment_nums) # if match_re: # comment_nums = match_re.group(1) # content = response.xpath("http://div[@class='entry']").extract()[0] #通過(guò)CSS選擇器提取字段 title = response.css(".entry-header h2::text").extract()[0] create_date = response.css(".entry-meta-hide-on-mobile::text").extract()[0].strip().replace("·","").strip() praise_numbers = response.css(".vote-post-up h20::text").extract()[0] fav_nums = response.css("span.bookmark-btn::text").extract()[0] match_re = re.match(".*?(\d+).*", fav_nums) if match_re: fav_nums = match_re.group(1) comment_nums = response.css("a[href='#article-comment'] span::text").extract()[0] match_re = re.match(".*?(\d+).*", comment_nums) if match_re: comment_nums = match_re.group(1) content = response.css("div.entry").extract()[0] tags = response.css("p.entry-meta-hide-on-mobile a::text").extract()[0] pass
以上是“CSS選擇器如何實(shí)現(xiàn)字段”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!