Html 准备多个URL以在R中使用rvest进行webscraping
我使用Html 准备多个URL以在R中使用rvest进行webscraping,html,r,string,csv,rvest,Html,R,String,Csv,Rvest,我使用rvest抓取多个URL时得到的结果不一致。连接的URL字符串返回一个字符向量。运行html\u节点将返回三个不同的结果 library(rvest) url <- c("https://interestingengineering.com/due-to-the-space-inside-atoms-you-are-mostly- made-up-of-empty-space", "https://futurism.com/mit-tech
rvest
抓取多个URL时得到的结果不一致。连接的URL字符串返回一个字符向量。运行html\u节点
将返回三个不同的结果
library(rvest)
url <- c("https://interestingengineering.com/due-to-the-space-inside-atoms-you-are-mostly-
made-up-of-empty-space",
"https://futurism.com/mit-tech-self-driving-cars-see-under-surface-road",
"https://techxplore.com/news/2020-02-socially-robot-children-autism.html",
"https://eos.org/science-updates/hackathon-speeds-progress-toward-climate-model-
collaboration",
"https://www.smithsonianmag.com/innovation/new-study-finds-people-prefer-robots-
explain-themselves-180974299/",
"https://www.sciencedaily.com/releases/2020/02/200227144259.htm")
page <-map(url, ~read_html(.x) %>% html_nodes("p") %>% html_text())
库(rvest)
url%html\u text())
此代码将返回从所有URL提取的内容
或者它将给出以下错误消息:
打开连接时出错(x,“rb”):
处理未编码内容时出错:设置的代码长度无效
或此错误消息:
wrapup期间出错:HTTP错误410
在最后一条错误消息之后,我还在控制台中得到Browse[1]>
我尝试从CSV文件运行URL:
urldoc<- read.csv("URLs for rvest.csv", stringsAsFactors=FALSE, sep = ",")
page <-map(urldoc, ~read_html(.x) %>% html_nodes("p") %>% html_text())
urldoc%html\u text())
print(urldoc)
输出看起来与级联代码中的输出类似,但我收到了不同的错误消息:
文档解析文件中出错(con,encoding=encoding,as\u html=as\u html,options=options):
应为单个字符串值:[type=character;extent=83]
我无法在数据框上运行html\u节点
或html\u文本
1) 如何获得无错误的一致返回。2) 更好的是,如何使用带有URL的文档而不是连接的字符串?您的第一个问题似乎是由URL上的换行符引起的 至于你的第二个问题:我可以从.csv复制你的问题。 这是我找到的解决办法
urldoc<- read.csv("URLs for rvest.csv", stringsAsFactors=FALSE, sep = ",", header=FALSE)
page <-map(urldoc[,1], ~read_html(.x) %>% html_nodes("p") %>% html_text())
urldoc%html\u text())
确保.csv每行只有一个URL,并指定要从中读取的列。当前的“URL”向量在URL本身中包含一些换行符,这将导致错误。一旦我纠正了,我就无法在上面重现你的错误。错误410“异常”表示页面无效,请仔细检查所有URL是否正确。最后一期,请确保每一个url都在一行上,并且在csv文件中每行只有一个url。谢谢!不幸的是,即使在清理csv后,我也会遇到同样的错误。它不会接受向量,而是需要一个字符串值。当我将它们串联起来运行时,它就工作了,但是我遇到了HTTP问题。我想当它遇到一个不工作的url时,刮擦就会停止。所以我需要找到一种方法来忽略这些url。谢谢。我对csv进行了一些清理,并确保使用URL对列进行子集设置。不幸的是,错误仍然存在。