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月起啟動漸進式縮表 維持基準利率不變']