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