[英]Error: invalid subscript type 'list' (Webscraping)
我正在嘗試從以下url-中進行數據爬網: https : //university.careers360.com/colleges/list-of-degree-colleges-in-India我想單擊每個大學名稱並獲取特定的數據每個學院。
首先,我要做的是將所有大學網址收集在一個向量中:
#loading the package:
library(xml2)
library(rvest)
library(stringr)
library(dplyr)
#Specifying the url for desired website to be scrapped
baseurl <- "https://university.careers360.com/colleges/list-of-degree-colleges-in-India"
#Reading the html content from Amazon
basewebpage <- read_html(baseurl)
#Extracting college name and its url
scraplinks <- function(url){
#Create an html document from the url
webpage <- xml2::read_html(url)
#Extract the URLs
url_ <- webpage %>%
rvest::html_nodes(".title a") %>%
rvest::html_attr("href")
#Extract the link text
link_ <- webpage %>%
rvest::html_nodes(".title a") %>%
rvest::html_text()
return(data_frame(link = link_, url = url_))
}
#College names and Urls
allcollegeurls<-scraplinks(baseurl)
#Reading the each url
library(purrr)
allreadurls<-map(allcollegeurls$url, read_html)
現在可以正常工作,但是當我編寫以下代碼時,它顯示了錯誤。
#Specialization
#Using CSS selectors to scrap the specialization section
allcollegeurls$Specialization<-NA
for (i in allreadurls) {
allcollegeurls$Specialization[i] <- html_nodes(allreadurls[i][],'td:nth-
child(1)')
}
Error in allreadurls[i] : invalid subscript type 'list'
我不確定所抓取的內容本身,但您可能希望將循環替換為
for (i in 1:length(allreadurls)) {
allcollegeurls$Specialization[i] <- html_nodes(allreadurls[i][],'td:nth-child(1)')
}
您的方法存在的一個問題是i
的角色不一致:它在allreadurls
中獲取值,但隨后allreadurls
Specialization
和allreadurls
用作子集。 另一個問題是所有多余的空間
'td:nth-
child(1)'
最后,由於allreadurls
是一個列表,因此您想使用[[i]]
而不是[i]
(它再次返回一個列表)來對其進行子集化。 最后,不需要[]
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.