2014-09-03 113 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