【问题标题】:How to scrape the data when there's missing values in selector nodes选择器节点中缺少值时如何刮擦数据
【发布时间】: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 页面,但仅用于将内容包含在公开可用的搜索引擎中的有限目的。”
  • 只是为了教育目的,别担心
  • 选择一个不同的网站。

标签: r rvest


【解决方案1】:

例如,您可以使用类似的东西。

data <- read_html("url.xml")


var <- data %>% html_nodes("//node") %>% xml_text() 

# observations that don´t have certain nodes - fill them with NA
var_pair <- data %>% html_nodes("node_var_pair") 

var_missing_clean = sapply(var_pair, function(x) {
  tryCatch(xml_text(html_nodes(x, "./var_missing")),
           error=function(err) NA)
})

df = data.frame(var, var_pair, var_missing)

您可以考虑三种类型的节点。 var 收集没有缺失数据的节点。 var_pair 包括要与包含缺失观察的节点配对的节点,var_missing 指的是缺失信息的节点。您可以创建变量并将它们聚合到数据数据框中 (df)

【讨论】:

  • 你确定网址没问题吗?它似乎在网站上不起作用
  • 这是那个ebay.in/sch/…
  • 例如:data = read_html("https://www.ebay.in/sch/i.html?_from=R40&amp;_sacat=0&amp;LH_ItemCondition=4&amp;_nkw=samsung%20j7&amp;LH_Complete=1&amp;LH_Sold=1&amp;rt=nc&amp;_trksid=p2045573.m1684") name &lt;- data %&gt;% html_nodes("h3") %&gt;% xml_text() %&gt;% as.data.frame() 这将为您提供名称。然后你需要弄清楚哪些节点包含你想要的信息。
【解决方案2】:

这里的过程很简单,分两步——首先提取块级别的所有节点(不是每个元素,也不要转换为文本)。这是一个长度等于块数的列表。第二个从这个提取的列表中提取每个元素作为文本并清理它。由于这是从列表中完成的,因此适用的 NA 会自动强制在正确的位置。查看来自同一 ebay 印度网站的示例:

library(rvest)
library(stringr)

# specify the url
url <-"https://www.ebay.in/sch/Mobile-Phones"

# read the page
web <- read_html(url)

# define the supernode that has the entire block of information
super_node <- '.li' 

# read as vector of all blocks of supernode (imp: use html_nodes function)
super_node_read <- html_nodes(web, super_node)

# define each node element that you want
node_model_details <- '.lvtitle'
node_description_1 <- '.lvtitle+ .lvsubtitle'
node_description_2 <- '.lvsubtitle+ .lvsubtitle'
node_model_price   <- '.prc .bold'
node_shipping_info <- '.bfsp'

# extract the output for each as cleaned text (imp: use html_node function)
model_details <- html_node(super_node_read, node_model_details) %>%
    html_text() %>%
    str_replace_all("[\t\n\r]" , "")

description_1 <- html_node(super_node_read, node_description_1) %>%
    html_text() %>%
    str_replace_all("[\t\n\r]" , "")

description_2 <- html_node(super_node_read, node_description_2) %>%
    html_text() %>%
    str_replace_all("[\t\n\r]" , "")

model_price  <- html_node(super_node_read, node_model_price) %>%
    html_text() %>%
    str_replace_all("[\t\n\r]" , "")

shipping_info <- html_node(super_node_read, node_shipping_info) %>%
    html_text() %>%
    str_replace_all("[\t\n\r]" , "")

# create the data.frame
mobile_phone_data <- data.frame(
    model_details,
    description_1,
    description_2,
    model_price,
    shipping_info
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多