starts-with 顾名思义,匹配一个属性开始位置的关键字
contains 匹配一个属性值中包含的字符串
text() 匹配的是显示文本信息,此处也可以用来做定位用
eg
//input[starts-with(@name,‘name1’)] 查找name属性中开始位置包含’name1’关键字的页面元素
//input[contains(@name,‘na’)] 查找name属性中包含na关键字的页面元素
xpath写法为 //a[text()=‘百度搜索’]
或者 //a[contains(text(),“百度搜索”)]
例如:
sel.xpath("//a[contains(., ‘Next Page’)]").extract()
[u’Click here to go to the Next Page’]
使用contains函数查找指定内容,格式为:
contains(str1, str2) # str1中有str2,则返回true,例如以下表达式输出链接文字中包含image的链接:
hxs.select(’//a[contains(@href, “image”)]/@href’).extract()
contains 是否当前节点或者属性 是否包括 哪些字符串
//div[@id=‘u1’]/a[@name=‘tj_login’]
//div[@id=‘u1’]/a[7]
//div/a[@name=‘tj_login’]
//div[contains(@id,‘u1’)]/a[contains(@name,‘tj_login’)]
//input[starts-with(@type,‘submit’)]
//input[contains(@type,‘submit’)]
//*[@id=“su”]
转自:https://blog.csdn.net/zhouxuan623/article/details/43935039