这篇文章主要介绍CSS选择器如何实现字段,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
创新互联公司成立于2013年,先为松北等服务建站,松北等地企业,进行企业商务咨询服务。为松北企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。根据上面所学的CSS基础语法知识,现在来实现字段的解析。首先还是解析标题。打开网页开发者工具,找到标题所对应的源代码。
发现是在div class="entry-header"
下面的h2节点中,于是打开scrapy shell 进行调试
但是我不想要
注意的是两个冒号。使用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"]/h2/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')]/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("·","").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选择器如何实现字段”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!