【发布时间】:2017-10-02 05:49:41
【问题描述】:
您好,我正在尝试在 R 中从 ebay 抓取数据,我使用了下面提到的代码,但遇到了一个问题,其中缺少特定选择器元素的值,为了绕过它,我使用了如图所示的 for 循环(检查每个列表并给出缺少数据的数字),因为抓取的数据较少,因此无法检查,但是当需要抓取大量数据时如何进行检查。 提前致谢
library(rvest)
url<-"https://www.ebay.in/sch/i.html_from=R40&_sacat=0&LH_ItemCondition=4&_ipg=100&_nkw=samsung+j7"
web<- read_html(url)
subdescp<- html_nodes(web, ".lvsubtitle+ .lvsubtitle")
subdescp1<-html_text(subdescp)
head(subdescp1)
library(stringr)
subdescp1<- str_replace_all(subdescp1, "[\t\n\r]" , "")
head(subdescp1)
for (i in c(5,6,10,19,33,34,35)){
a<-subdescp1[1:(i-1)]
b<-subdescp1[i:length(subdescp1)]
subdescp1<-append(a,list("NA"))
subdescp1<-append(subdescp1,b)
}
Z<-as.character(subdescp1)
Z
webpage <- read_html(url)
Descp_data_html <- html_nodes(webpage,'.vip')
Descp_data <- html_text(Descp_data_html)
head(Descp_data)
price_data_html <- html_nodes(web,'.prc .bold')
price_data <- html_text(price_data_html)
head(price_data)
library(stringr)
price_data<-str_replace_all(price_data, "[\t\n]" , "")
price_data<-gsub("Rs. ","",price_data)
price_data<-gsub(",","",price_data)
price_data<- as.numeric(price_data)
price_data
Desc_data_html <- html_nodes(webpage,'.lvtitle+ .lvsubtitle')
Desc_data <- html_text(Desc_data_html, trim = TRUE)
head(Desc_data)
j7_f2<-data.frame(Title = Descp_data, Description= Desc_data, Sub_Description= Z, Pirce = price_data)
【问题讨论】:
-
“未经 eBay 明确许可,严禁使用机器人或其他自动化方式访问 eBay 网站。尽管有上述规定,eBay 可能允许自动访问某些 eBay 页面,但仅用于将内容包含在公开可用的搜索引擎中的有限目的。”
-
只是为了教育目的,别担心
-
选择一个不同的网站。