根據上面所學的CSS基礎語法知識,現在來實作欄位的解析。首先還是解析標題。開啟網頁開發者工具,找到標題所對應的原始碼。本文主要介紹了CSS選擇器實現字段解析的相關資料,需要的朋友可以參考下,希望能幫助到大家
發現是在p class= "entry-header"
下面的h1節點中,於是打開scrapy shell 進行調試
但是我不想要4a249f0d628e2318394fd9b75b4636b1這種標籤該咋辦,這時候就要使用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('//p[@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("//p[@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("p.entry").extract()[0] tags = response.css("p.entry-meta-hide-on-mobile a::text").extract()[0] pass
相關推薦:
以上是CSS選擇器欄位解析的實作方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!