Home | 简体中文 | 繁体中文 | 杂文 | 打赏(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎专栏 | Search | Email

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月起启动渐进式缩表 维持基准利率不变']