首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R循环遍历每个URL,刮取、解析、提取节点并放入数据帧

R循环遍历每个URL,刮取、解析、提取节点并放入数据帧
EN

Stack Overflow用户
提问于 2019-02-13 14:33:56
回答 1查看 639关注 0票数 1

使用以下包: require(stringr) require(RCurl) require(XML) 我能够连接到所需的网页,并提取所需的信息。

代码语言:javascript
复制
> url="https://www.realtor.com/realestateagents/33415/pg-1" doc =
> getURLContent(url, verbose = TRUE) #gets the doc , verbose = show me
> me what you are doing) doc = htmlParse(doc)
> # name =  getNodeSet(doc,  "//div[@itemprop = 'name']") name = sapply(name, xmlValue)
> # phone =  getNodeSet(doc,  "//div[@itemprop= 'telephone']") phone = sapply(phone, xmlValue)

我生成了一个urls列表

代码语言:javascript
复制
urlList = c("https://www.realtor.com/realestateagents/33415/pg-1",
                "https://www.realtor.com/realestateagents/33415/pg-2")

    urlList = as.list(urlList)

我想循环每个url,捕获相同的节点,并将结果放在一个由名和电话列组成的数据框架中。 我试了一下,但没有成功。

代码语言:javascript
复制
Reduce(function(...) merge(..., all=T), 
       lapply(urls_list, function(x) {
         data.frame(urlList=x, 

                     # d<- htmlParse(getURLContent(x))
                    d<-htmlParse(d)
                    d1 =  getNodeSet(d,  "//div[@itemprop = 'name']")
                    name = sapply(name, xmlValue)

       })) -> results

谢谢你的帮助

EN

回答 1

Stack Overflow用户

发布于 2019-02-13 17:12:46

我觉得像这样的东西应该能帮你弄到你想要的信息。

代码语言:javascript
复制
library(rvest)

zip.codes <- c("33415", "33413")

results <- list()

result.index <- 0

for(zip in zip.codes){

  url <- paste0("https://www.realtor.com/realestateagents/", zip ,"/pg-1" )

  page <- read_html(url)

  max.pages <- as.numeric(max(page %>% 
                                html_nodes(xpath = '//*[@class="page"]') %>% 
                                html_nodes("a") %>% 
                                html_text))

  for(i in c(1:max.pages)){
    print(paste("Processing Zip Code", zip, "- Page", i, "of", max.pages))

    result.index <- result.index + 1

    url <- paste0("https://www.realtor.com/realestateagents/", zip,"/pg-", i)

    page <- read_html(url)

    df <- data.frame(AgentID = page %>% 
                               html_nodes(xpath = '//*[@id="call_inquiry_cta"]') %>% 
                               xml_attr("data-agent-id"),
                     AgentName = page %>% 
                               html_nodes(xpath = '//*[@id="call_inquiry_cta"]') %>% 
                               xml_attr("data-agent-name"),
                     AgentAddr = page %>% 
                               html_nodes(xpath = '//*[@id="call_inquiry_cta"]') %>% 
                               xml_attr("data-agent-address"),
                     AgentPhone = sub("tel:", "", page %>% 
                                                  html_nodes(xpath = '//*[@id="call_inquiry_cta"]') %>% 
                                                  xml_attr("href")),
                     PhoneType = page %>% 
                                 html_nodes(xpath = '//*[@id="call_inquiry_cta"]') %>% 
                                 xml_attr("data-agent-num-type"),
                     AgentWebSite = page %>% 
                                    html_nodes(xpath = '//*[@id="call_inquiry_cta"]') %>% 
                                    xml_attr("data-agent-web-url"))

    results[[result.index]] <- df
  }
}

df <- do.call(rbind, results)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54672672

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档