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)
我怎麼能匹配,並且只返回非本市元素呢?