使用getURIAsynchronous()抓取多个网页
我是R方面的新手。我正在尝试使用使用getURIAsynchronous()抓取多个网页,r,web-scraping,rcurl,R,Web Scraping,Rcurl,我是R方面的新手。我正在尝试使用RCurl包中的getURIAsynchronous()函数刮取多个https网页。但是,对于每个url,函数都返回“”作为结果 我尝试从同一个包中使用url.exists()函数来查看它是否返回TRUE或FALSE。令我惊讶的是,它返回的值为FALSE。但是url是存在的 由于我使用的这些https URL是我公司特定的URL,因此出于保密原因,我无法在此提供示例。但是,使用readLines()可以成功地从网站中提取所有html内容。但对于成千上万的URL来说
RCurl
包中的getURIAsynchronous()
函数刮取多个https网页。但是,对于每个url,函数都返回“”作为结果
我尝试从同一个包中使用url.exists()
函数来查看它是否返回TRUE或FALSE。令我惊讶的是,它返回的值为FALSE。但是url是存在的
由于我使用的这些https URL是我公司特定的URL,因此出于保密原因,我无法在此提供示例。但是,使用readLines()
可以成功地从网站中提取所有html内容。但对于成千上万的URL来说,这既慢又耗时。知道为什么getURIAsynchronous()
返回“”而不是删除html内容吗?我在这里的重点是只抓取整个html内容,我可以自己解析数据
有没有其他软件包可以帮助我更快地浏览多个https网站,而不是一次只浏览一个页面
更新:下面是一个小例子,类似于我一直试图做的。在这种情况下,它只是几个网址刮,但在我的项目中,我有几千个。当我尝试使用下面类似的代码提取文本时,所有URL都会得到“”
图书馆(RCurl)
source_url我不知道您正试图从哪个特定的url中获取数据,但下面的代码将演示如何循环遍历多个url,并从每个url中获取数据。也许您可以利用此代码来实现您的特定目标
库(rvest)
图书馆(stringr)
#创建一个主数据框来存储所有结果
非常感谢!实际上,我正在尝试使用RCurl包在R中执行并行web抓取。我可以使用readLines()函数来完成这项工作,而且工作非常完美。但它很慢,而且需要时间,因为它一次只能抓取一个url。我正在尝试使用RCurl的getURIAsynchronous()函数进行并行web抓取。我在组织外的一些网站上尝试过,比如IMDB,它做得更好。但由于某些原因,我无法刮取任何特定于我的组织的https页面。如果我能做到这一点,我的处理时间就会减少一半。
library(rvest)
library(stringr)
#create a master dataframe to store all of the results
complete <- data.frame()
yearsVector <- c("2010", "2011", "2012", "2013", "2014", "2015")
#position is not needed since all of the info is stored on the page
#positionVector <- c("qb", "rb", "wr", "te", "ol", "dl", "lb", "cb", "s")
positionVector <- c("qb")
for (i in 1:length(yearsVector)) {
for (j in 1:length(positionVector)) {
# create a url template
URL.base <- "http://www.nfl.com/draft/"
URL.intermediate <- "/tracker?icampaign=draft-sub_nav_bar-drafteventpage-tracker#dt-tabs:dt-by-position/dt-by-position-input:"
#create the dataframe with the dynamic values
URL <- paste0(URL.base, yearsVector[i], URL.intermediate, positionVector[j])
#print(URL)
#read the page - store the page to make debugging easier
page <- read_html(URL)
#find records for each player
playersloc <- str_locate_all(page, "\\{\"personId.*?\\}")[[1]]
# Select the first column [, 1] and select the second column [, 2]
players <- str_sub(page, playersloc[, 1] + 1, playersloc[, 2] - 1)
#fix the cases where the players are named Jr.
players <- gsub(", ", "_", players)
#split and reshape the data in a data frame
play2 <- strsplit(gsub("\"", "", players), ',')
data <- sapply(strsplit(unlist(play2), ":"), FUN = function(x) { x[2] })
df <- data.frame(matrix(data, ncol = 16, byrow = TRUE))
#name the column names
names(df) <- sapply(strsplit(unlist(play2[1]), ":"), FUN = function(x) { x[1] })
#store the temp values into the master dataframe
complete <- rbind(complete, df)
}
}
library(rvest)
library(stringr)
library(tidyr)
site <- 'http://www.basketball-reference.com/play-index/draft_finder.cgi?request=1&year_min=2001&year_max=2014&round_min=&round_max=&pick_overall_min=&pick_overall_max=&franch_id=&college_id=0&is_active=&is_hof=&pos_is_g=Y&pos_is_gf=Y&pos_is_f=Y&pos_is_fg=Y&pos_is_fc=Y&pos_is_c=Y&pos_is_cf=Y&c1stat=&c1comp=&c1val=&c2stat=&c2comp=&c2val=&c3stat=&c3comp=&c3val=&c4stat=&c4comp=&c4val=&order_by=year_id&order_by_asc=&offset=0'
webpage <- read_html(site)
draft_table <- html_nodes(webpage, 'table')
draft <- html_table(draft_table)[[1]]
jump <- seq(0, 800, by = 100)
site <- paste('http://www.basketball-reference.com/play-index/draft_finder.cgi?',
'request=1&year_min=2001&year_max=2014&round_min=&round_max=',
'&pick_overall_min=&pick_overall_max=&franch_id=&college_id=0',
'&is_active=&is_hof=&pos_is_g=Y&pos_is_gf=Y&pos_is_f=Y&pos_is_fg=Y',
'&pos_is_fc=Y&pos_is_c=Y&pos_is_cf=Y&c1stat=&c1comp=&c1val=&c2stat=&c2comp=',
'&c2val=&c3stat=&c3comp=&c3val=&c4stat=&c4comp=&c4val=&order_by=year_id',
'&order_by_asc=&offset=', jump, sep="")
dfList <- lapply(site, function(i) {
webpage <- read_html(i)
draft_table <- html_nodes(webpage, 'table')
draft <- html_table(draft_table)[[1]]
})
finaldf <- do.call(rbind, dfList)