欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

CSS選擇器實現(xiàn)字段解析

  發(fā)布時間:2018-01-31 16:21:05   作者:佚名   我要評論
這篇文章主要介紹了CSS選擇器實現(xiàn)字段解析的相關(guān)資料,需要的朋友可以參考下

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

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

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

注意的是兩個冒號。使用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"]/h1/text()').extract()[0]  
        # create_date = response.xpath("//p[@class = 'entry-meta-hide-on-mobile']/text()").extract()[0].strip().replace("·","").strip()  
        # praise_numbers = response.xpath("//span[contains(@class,'vote-post-up')]/h10/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 h1::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 h10::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  

總結(jié)

以上所述是小編給大家介紹的CSS選擇器實現(xiàn)字段解析,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論