CSS選擇器如何實現(xiàn)字段-創(chuàng)新互聯(lián)

這篇文章主要介紹CSS選擇器如何實現(xiàn)字段,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

創(chuàng)新互聯(lián)公司成立于2013年,先為松北等服務建站,松北等地企業(yè),進行企業(yè)商務咨詢服務。為松北企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務解決您的所有建站問題。

根據(jù)上面所學的CSS基礎語法知識,現(xiàn)在來實現(xiàn)字段的解析。首先還是解析標題。打開網(wǎng)頁開發(fā)者工具,找到標題所對應的源代碼。

CSS選擇器如何實現(xiàn)字段

發(fā)現(xiàn)是在div class="entry-header"下面的h2節(jié)點中,于是打開scrapy shell 進行調(diào)試

CSS選擇器如何實現(xiàn)字段

但是我不想要<h2>這種標簽該咋辦,這時候就要使用CSS選擇器中的偽類方法。如下所示。

CSS選擇器如何實現(xiàn)字段

注意的是兩個冒號。使用CSS選擇器真的很方便。同理我用CSS實現(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("//p[@class = 'entry-meta-hide-on-mobile']/text()").extract()[0].strip().replace("&middot;","").strip()  
        # praise_numbers = response.xpath("//span[contains(@class,'vote-post-up')]/h20/text()").extract()[0]  
        # fav_nums = response.xpath("//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("//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("//div[@class='entry']").extract()[0]  
#通過CSS選擇器提取字段  
        title = response.css(".entry-header h2::text").extract()[0]  
        create_date = response.css(".entry-meta-hide-on-mobile::text").extract()[0].strip().replace("&middot;","").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選擇器如何實現(xiàn)字段

以上是“CSS選擇器如何實現(xiàn)字段”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

網(wǎng)站名稱:CSS選擇器如何實現(xiàn)字段-創(chuàng)新互聯(lián)
轉(zhuǎn)載來于:http://muchs.cn/article26/cdghcg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設響應式網(wǎng)站、網(wǎng)站設計品牌網(wǎng)站設計、面包屑導航、用戶體驗

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作