Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | 51CTO学院 | CSDN程序员研修院 | OSChina 博客 | 腾讯云社区 | 阿里云栖社区 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏多维度架构

11.6. xpath

11.6.1. 逻辑运算符

11.6.1.1. and

				
>>> response.xpath('//span[@class="time" and @id="news-time"]/text()').extract()
['2017-10-09 09:46']				
				
				

11.6.1.2. or

				
//*[@class='foo' or contains(@class,' foo ') or starts-with(@class,'foo ') or substring(@class,string-length(@class)-3)=' foo']				
				
				

11.6.2. function

11.6.2.1. text()

				
>>> response.xpath('//title/text()').extract_first()
'Netkiller ebook - Linux ebook'				
				
				

11.6.2.2. contains()

contains() 匹配含有特定字符串的 class

				
//*[contains(@class,'foo')]
				
				
				
>>> response.xpath('//ul[contains(@class, "topnews_nlist")]/li/h2/a/@href|//ul[contains(@class, "topnews_nlist")]/li/a/@href').extract()	
				
				

内容匹配

				
>>> response.xpath('//div[@id="epContentLeft"]/h1[contains(text(),"10")]/text()').extract()
['美联储10月起启动渐进式缩表 维持基准利率不变']