4
我是XPath新手 - 請對我輕鬆點。R XML + XPath - 具有多個條件的getNodeSet
無法在我的目標頁面上爲沒有大量結構的元素抽取XPath。
該數據集是NJ學校report cards。個人成績單看起來像this
我已經找到了如何拉出來的是有一個summary
標籤表:
url <- paste("http://education.state.nj.us/rc/rc11/rcreport.php?c=",
all_sch[i,1],";d=",all_sch[i,2],";s=",all_sch[i,3],sep = '')
doc = htmlParse(url)
admin_salaries = getNodeSet(doc, '//table[@summary="Administrative Salaries and Benefits"]')
但我有麻煩那裏沒有很多額外的識別信息工作關閉。
例如,有校名和區表看起來是這樣的:在這裏
<table cellpadding="0" cellspacing="0">
<tr>
<td><strong>SCHOOL:</strong></td>
<td> New Jersey Ave</td>
</tr>
<tr>
<td><strong>COUNTY:</strong></td>
<td> Atlantic</td>
</tr>
<tr>
<td><strong>DISTRICT:</strong></td>
<td> Atlantic City</td>
</tr>
</table>
我的策略是「發現是表,並有文字COUNTY
Reading,就像我的節點可有關XPath的,我想這一點:
names = getNodeSet(doc,'//table and //*[contains(text(),"COUNTY")]')
但不是返回回表節點,它給了我一個布爾價值。
因此,問題是:如何使用XPath查找具有COUNTY和SCHOOL文本的表?
我已經嘗試了很多其他的策略,沒有多大用處。其他人提出的一個方法是簡單地使用像這樣拉出來的每個表數據單元格:
xpathApply(htmlTreeParse(url, useInt=T), "//td", function(x) xmlValue(x))
但模板不丟失數據是一致的 - 不完整的報告有相當不同的結構,元件不跨越2000多頁的相同位置。
任何幫助,非常感謝!
完美。謝謝! – Andrew