from scrapy.selector import Selector
# -------css----------------
css选择器http://www.w3school.com.cn/cssref/css_selectors.asp
selector = Selector(text=response.text)
selector.css(".x.y") # 选择两个属性
selector.css(".bar::attr(title)").extract()[0] # 选择.bar的title值
selector.css("td::text").extract()[0] # 选择td的文字
# -------------------xpath------------------
文档 http://www.w3school.com.cn/xpath/xpath_syntax.asp
selector.xpath('//div[@class="entry-header"]/h1/text()').extract()[0]
selector.xpath("//span[contains(@class,'votepostup')]/text()").extract()[0]
# contains 匹配一个属性值中包含的字符串
scrapy Selector 选择器
最新推荐文章于 2024-08-26 19:13:51 发布