2014-09-03 108 views
1

从这个站点http://www.gwblawfirm.com/contact-us/下面的xpath调用提取24个元素。但我只想要四个城市元素(安德森,夏洛特,哥伦比亚和格林维尔 - 12:15)。如果国家出现机智,那么这个城市就是好的。xpath表达式匹配位置范围或位置子集

require(XML) 
doc <- htmlTreeParse('http://www.gwblawfirm.com/contact-us/', useInternal = TRUE)   
xpathSApply(doc, "//ul[@class='menu']/li/a", xmlValue, trim = TRUE) 
[1] "Home"    "About"    "Staff"    "Abnormal Use Blog" "Contact Us"  
[6] "Attorneys"   "Practice Areas" "Industries"  "News"    "Resources"   
[11] "Career Center"  "Anderson, SC"  "Charlotte, NC"  "Columbia, SC"  "Greenville, SC" 
[16] "Home"    "Attorneys"   "Practice Areas" "Industries"  "About"    
[21] "News"    "Career Center"  "Contact Us"  "Disclaimer" 

这个问题提出了以下,但它返回所有24 properly express the node range from 3 to 10

xpathSApply(doc, "//ul[@class='menu']/li/a[position()>=1 and position()<=16]", xmlValue, trim = TRUE) 

我怎么能匹配,并且只返回非本市元素呢?

回答

1

您需要使用方括号标识整个XPath的结果中<a>的位置,否则position()公认的局部位置相同<li>其父节点中:

(//ul[@class='menu']/li/a)[position()>=12 and position()<=15] 

一个更好的选择是基于<h2 class="widgettitle">Contact</h2>标记获取<ul>

//h2[@class='widgettitle' and .='Contact']/following-sibling::ul[@class='menu'][1]/li/a