使用r-我想从网站提取一些表格数据
我在网站上刮擦数据时遇到了一些问题。我在网上剪贴式上没有很多经验。我的预期计划是使用以下网站的R刮擦一些数据: https://www.fatf---fatf--- gafi.org/countries/
更确切地说,我想提取具有某种制裁的国家列表
library(XML)
url <- paste0("https://www.fatf-gafi.org/countries/")
source <- readLines(url, encoding = "UTF-8")
parsed_doc <- htmlParse(source, encoding = "UTF-8")
,但这不会带来预期的信息,因为不在桌子旁,而是嵌套的Div。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只是为了测试JavaScript评估如何与V8,嵌入式JavaScript和WebAssembly Engine一起使用。
创建上下文引擎,评估请求的JavaScript并从V8中获取
nistry> nistry>变量的值(它已变成嵌套的dataframe,因此
nest(
nest)(
nest)( )
),最后一行充满了na
s,因此是过滤器。Just to test how JavaScript evaluation works with V8, Embedded JavaScript and WebAssembly Engine.
https://cran.r-project.org/web/packages/V8/vignettes/v8_intro.html
Create context engine, evaluate requested JavaScript and get the value of
countries
variable from V8 (it's turned into nested dataframe, thus theunnest()
), last row is filled withNA
s, thus the filter.这是一项棘手的解析工作。您需要的信息不在您从
readlines
中获得的HTML中。而是使用XHR请求通过页面动态加载它。通常,像这样的XHR请求会返回JSON字符串,但是在您的情况下,它返回JavaScript,其中将信息存储为一个包含JSON片段的变量,每个国家 /地区一个。可以通过一些字符串操纵和JSON解析来访问这一点,以获得最终结果:This is a tricky parsing job. The information you need is not in the html you are getting from
readLines
. Instead, it is loaded dynamically by the page using an XHR request. Often, an XHR request like this will return a json string, but in your case it returns javascript where the information is stored as a variable containing an array of json snippets, one for each country. This can be accessed through some string manipulation and json parsing to get your end result: