下载mp3文件

R用户

我想使用R的网站。该网站是http://soundoftext.com/,可以在其中下载WAV。带有来自给定文本和语言(语音)的音频的文件。

下载WAV语音有两个步骤:1)插入文本和选择语言。并提交2)在新窗口中,单击保存并选择文件夹。

到目前为止,我可以获取xml树,将其转换为列表,并修改文本和语言的值。但是,我不知道如何将列表转换为XML(具有新值)并执行它。然后,我也需要执行第二步。

到目前为止,这是我的代码:

require(RCurl)
require(XML)
webpage <- getURL("http://soundoftext.com/")
webpage <- readLines(tc <- textConnection(webpage)); close(tc)
pagetree <- htmlTreeParse(webpage, error=function(...){}, useInternalNodes = TRUE)
x<-xmlToList(pagetree)
# Inserting word
x$body$div$div$div$form$div$label$.attrs[[1]]<-"Raúl"
x$body$div$div$div$form$div$label$.attrs[[1]]

# Select language
x$body$div$div$div$form$div$select$option$.attrs<-"es"
x$body$div$div$div$form$div$select$option$.attrs 

我已经遵循这种方法,但是“标签”存在错误。

更新:我只是试图使用rvest下载音频文件,但是,它没有响应或触发任何东西。我做错了什么(想念)?

url <- "http://soundoftext.com/"
s <- html_session(url)
f0 <- html_form(s)
f1 <- set_values(f0[[1]], text="Raúl", lang="es")
attr(f1, "type") <- "Submit"
s[["fields"]][["submit"]] <- f1
attr(f1, "Class") <- "save"

test <- submit_form(s, f1)
甘巴

我认为您的方法没有问题,值得尝试。这也是我要写的内容。
该页面有点烦人,它使用jquery在每个请求后附加新的div。我仍然认为应该可以使用rvest,但我发现了使用该httr的有趣解决方法

library(httr)    

url <- "http://soundoftext.com/sounds"

fd <- list(
  submit = "save",
  text = "Banana", 
  lang="es"
)

resp<-POST(url, body=fd, encode="form")
id <- content(resp)$id

download.file(URLencode(paste0("http://soundoftext.com/sounds/", id)), destfile = 'test.mp3')

从本质上讲,当我们将POST请求发送到服务器时ID,只要我们可以GETid何时下载文件就可以返回

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章