Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.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
Html 在R中从维基百科抓取URL只产生一半的URL_Html_R_Url_Web_Scrape - Fatal编程技术网

Html 在R中从维基百科抓取URL只产生一半的URL

Html 在R中从维基百科抓取URL只产生一半的URL,html,r,url,web,scrape,Html,R,Url,Web,Scrape,我目前正试图从维基百科页面提取URL,其中包含首席执行官列表,然后代码打开URL并将文本复制到.txt文件中供我使用。问题在于allurls对象只包含URL的后半部分。例如,allurls[1]给出了“/wiki/Pierre_Nanterme”。因此,当我运行此代码时 library("xml2") library("rvest") url <- "https://en.wikipedia.org/wiki/List_of_chief_executive_officers" allu

我目前正试图从维基百科页面提取URL,其中包含首席执行官列表,然后代码打开URL并将文本复制到.txt文件中供我使用。问题在于
allurls
对象只包含URL的后半部分。例如,
allurls[1]
给出了“/wiki/Pierre_Nanterme”。因此,当我运行此代码时

library("xml2")
library("rvest")

url <- "https://en.wikipedia.org/wiki/List_of_chief_executive_officers"

allurls <- url %>% read_html() %>% html_nodes("td:nth-child(2) a") %>% 
html_attr("href") %>% 
  .[!duplicated(.)]%>%lapply(function(x) 
read_html(x)%>%html_nodes("body"))%>%  
  Map(function(x,y) 
write_html(x,tempfile(y,fileext=".txt"),options="format"),.,
      c(paste("tmp",1:length(.))))

allurls[1]
库(“xml2”)
图书馆(“rvest”)
url%html\u节点(“td:N子(2)a”)%>%
html_attr(“href”)%%>%
[!重复(.)]%>%lappy(函数(x)
已读html(x)%%>%html\U节点(“正文”)%%>%
映射(函数(x,y)
写入html(x,tempfile(y,fileext=“.txt”),options=“format”),。,
c(粘贴(“tmp”,1:长度)
allurls[1]
我得到以下错误:

错误:“/wiki/Pierre_Nanterme”不存在


您需要添加
https://en.wikipedia.org
在前面。是的,但我如何才能在所有179个条目前面添加该内容?在代码的中间?我已经尝试创建一个新对象,但无法在
read_html
read_html(粘贴0(“https://en.wikipedia.org“,x))%%>%html\u节点(“正文”)%%>%
。也就是说,为了清晰起见,您需要缩进管道或根本不缩进管道。此外,您的刮取html_attr包含以
https
Ah yes开头的不正确元素!谢谢,这就解决了!现在唯一的错误似乎来自您指出的错误元素