【发布时间】:2023-03-07 08:04:02
【问题描述】:
我正在使用 XML 和 html 包进行一些网络抓取,我需要隔离国家名称以及您在下面看到的两个数值:
<tr><td>Tonga</td>
<td class="RightAlign">3,000</td>
<td class="RightAlign">6,000</td>
</tr>
这是我到目前为止编写的代码 - 我认为我只需要正确的正则表达式?
# a vector to store the results
pages<-character(0)
country_names<-character(0)
# go through all 6 pages containing the info we want, and store
# the html in a list
for (page in 1:6) {
who_search <- paste(who_url, page, '.html', sep='')
page = htmlTreeParse(who_search, useInternalNodes = T)
pages=c(page, pages)
# extract the country names of each tweet
country <- xpathSApply(page, "????", xmlValue)
country_names<-c(country, country_names)
}
【问题讨论】:
-
你能发布其中一个页面的 URL 吗?如果没有看到完整的页面,就不可能告诉您正确的 XPath 是什么,因为可能该页面包含的表格行比您发布的要多。
-
who_url who.int/diabetes/facts/world_figures/en/index"
标签: r xml-parsing web-scraping