Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从R中使用JavaScript刮取页面的数据链接_Javascript_Html_R_Web Scraping - Fatal编程技术网

从R中使用JavaScript刮取页面的数据链接

从R中使用JavaScript刮取页面的数据链接,javascript,html,r,web-scraping,Javascript,Html,R,Web Scraping,请帮帮我 我是新的网页刮在R。我想收集链接 在此页面上下载数据表。我的项目是让这些数据更容易访问 以下是网站: 在Donnée页面中,我有一个扇区列表。 农业:43个表 公众帮助:7张表格 当我点击农业时,我得到了数据集列表。 我想得到这些标题的列表。 对于每个标题,获取数据集的下载链接。 我尝试了下面的代码来查看站点的结构。 但我没有看到允许我提取这些链接的体系结构 library(RCurl) library(XML) library(rvest) URL <- "http://b

请帮帮我

我是新的网页刮在R。我想收集链接 在此页面上下载数据表。我的项目是让这些数据更容易访问

以下是网站:

在Donnée页面中,我有一个扇区列表。 农业:43个表 公众帮助:7张表格

当我点击农业时,我得到了数据集列表。

我想得到这些标题的列表。 对于每个标题,获取数据集的下载链接。 我尝试了下面的代码来查看站点的结构。 但我没有看到允许我提取这些链接的体系结构

library(RCurl)
library(XML)
library(rvest)
URL <- "http://burkinafaso.opendataforafrica.org/"
pg <- read_html(URL)
p <- html_children(pg)[1]
pp <- html_children(pg)[2]
html_structure(p)
html_structure(pp)
例如,我在标记中的链接中尝试了这段代码。 但是我没有得到不同的下载链接

URL <- "http://burkinafaso.opendataforafrica.org/data/#topic=Agriculture"
pg <- read_html(URL)
all.url <- html_attr(html_nodes(pg, "a"), "href")
all.url <- as.data.frame(all.url)
正如我预期的那样,对于每个itm 表格列表和下载链接。例如:

公共援助7:

标签链接

ExtExeReeLePaCeReCe 1995,2006 助手ExpReeRePar类型EN数百万FCFA下载链接

请帮助我。

网络流量和API调用:

比如说,如果你从

http://burkinafaso.opendataforafrica.org/data/menu=topic

您可以看到所有顶级链接的列表及其数据集的计数。如果我们点击Aide Publique 7,我们将看到7个部分,如果您点击任何部分,则会显示Select数据集

如果您在第一次单击时监控web流量,您将看到API POST请求,用于检索Aide Publique 7的数据:

如果我们进一步检查请求,我们可以观察url中的查询字符串参数和请求负载:

参数基本上是关于我们的一些信息,我们可以删除;对有效载荷的一些实验表明,如果我们排除有效载荷,我们实际上得到了所有的主题,而不仅仅是Aide Publique 7

API响应:

现在,响应是json,是一个数组,但在R中表示列表。以列表中一个项目的一部分为例:

该信息与实际数据集链接的比较,例如

单击Select数据集将生成

http://burkinafaso.opendataforafrica.org/nthpfqd/aide-ext%C3%A9rieure-par-secteur-de-1995-%C3%A0-2006-en-MELLONS-de-fcfa

与JSON的列表项1之前的图像进行快速比较表明,如果我们认为这个新URL解码为:

http://burkinafaso.opendataforafrica.org/nthpfqd/aide-ext1995年至2006年,fcfa百万欧元

格式如下:

"http://burkinafaso.opendataforafrica.org/{item[id]}/{item[title]}'

这意味着,在json响应对象上的循环中,我们可以通过将基本字符串与当前项id和当前项标题连接起来来生成最终链接。我们还可以从当前项目名称中提取标题。我们可以使用purrr和map_df处理循环和最终数据帧生成,并使用httr生成POST

R:


非常感谢您的快速回复@QHarr。它已经帮了我很多。这回答了我的问题。现在我如何下载我的数据。我做了一个函数来下载第一个链接。但是他给我发了html xml代码。下面是脚本检索到的第一个链接:链接我制作了这段代码来下载它:下载图像我提取的链接我不认为会启动下载。我看不出有什么会这样。你能告诉我你期望的下载链接是什么吗?我想要这个数据集的下载链接:当你点击链接时,右边有一个按钮,可以让你下载表格:对于这个页面,我看到的是:
URL <- "http://burkinafaso.opendataforafrica.org/data/#topic=Agriculture"
pg <- read_html(URL)
all.url <- html_attr(html_nodes(pg, "a"), "href")
all.url <- as.data.frame(all.url)
library(httr)
library(purrr)

r <- content(POST("http://burkinafaso.opendataforafrica.org/api/1.0/meta/dataset"))

df <- map_df(r, function(item) {

  data.frame(title = item$name,
             url = paste0("http://burkinafaso.opendataforafrica.org/", item$id,'/',item$title),
             stringsAsFactors=FALSE)
})

View(df)
import requests
import pandas as pd

r = requests.post('http://burkinafaso.opendataforafrica.org/api/1.0/meta/dataset').json()
df = pd.DataFrame([(item['name'], f'http://burkinafaso.opendataforafrica.org/{item["id"]}/{item["title"]}') for item in r]
                  ,columns = ['Title','Url'])
print(df)