意见箱
恒创运营部门将仔细参阅您的意见和建议,必要时将通过预留邮箱与您保持联络。感谢您的支持!
意见/建议
提交建议

在CSS中用基础语法知识怎样实现字段的解析

来源:恒创科技 编辑:恒创科技编辑部
2024-01-11 18:51:59
今天这篇给大家分享的知识是“在CSS中用基础语法知识怎样实现字段的解析”,小编觉得挺不错的,对大家学习或是工作可能会有所帮助,对此分享发大家做个参考,希望这篇“在CSS中用基础语法知识怎样实现字段的解析”文章能帮助大家解决问题。

根据上面所学的CSS基础语法知识,现在来实现字段的解析。首先还是解析标题。打开网页开发者工具,找到标题所对应的源代码。


在CSS中用基础语法知识怎样实现字段的解析

发现是在div class="entry-header"下面的h1节点中,于是打开scrapy shell 进行调试

但是我不想要<h1>这种标签该咋办,这时候就要使用CSS选择器中的伪类方法。如下所示。

注意的是两个冒号。使用CSS选择器真的很方便。同理我用CSS实现字段解析。代码如下

# -*- 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  

总结


以上就是关于“在CSS中用基础语法知识怎样实现字段的解析”的介绍了,感谢各位的阅读,如果大家想要了解更多相关的内容,欢迎关注群英网络,小编每天都会为大家更新不同的知识。
上一篇: CSS中定位属性是什么,怎么实现多种定位效果 下一篇: 手机怎么远程登录云服务器?