library(jsonlite) library(rvest) library(stringr) herolist <- fromJSON('http://pvp.qq.com/web201605/js/herolist.json')[,c(1:2,5)] item <- fromJSON('http://pvp.qq.com/web201605/js/item.json')[,1:2] ming <- fromJSON('http://pvp.qq.com/web201605/js/ming.json')[,c(1,4)] summoner <- fromJSON('http://pvp.qq.com/web201605/js/summoner.json')[,1:2] url0 <- 'http://pvp.qq.com/web201605/herodetail/' scrapy_fun <- function(id){ url <- paste0(url0,id,'.shtml') web <- read_html(url) #英雄描述 discribe <- web%>%html_nodes('.cover > ul > li> span >i')%>%html_attr('style')%>% str_extract('[0-9]{1,}') base <- data.frame(生存能力=discribe[1], 攻击伤害=discribe[2], 技能效果=discribe[3], 上手难度=discribe[4], stringsAsFactors =F) #组合 couple <- web%>%html_nodes('li > a')%>%html_attr('href')%>% str_extract('[0-9]{1,}')%>%as.numeric() #最佳搭档 cop1 <- herolist$cname[which(herolist$ename%in%couple[1:2])] #压制英雄 cop2 <- herolist$cname[which(herolist$ename%in%couple[3:4])] #被压制英雄 cop3 <- herolist$cname[which(herolist$ename%in%couple[5:6])] #建议铭文 sugg_mingx <- web%>%html_nodes('.sugg-u1')%>%html_attr('data-ming')%>% str_split('\\|')%>%unlist()%>%as.numeric() sugg_ming=ming$ming_name[which(ming$ming_id%in%sugg_mingx)] #技能 skill_name <- web%>%html_nodes('.skill-name>b')%>%html_text() #推荐召唤师 sugg_name3 <- web%>%html_nodes('.sugg-name.sugg-name3>span')%>%html_text() #出装1 equip_list_f1 <- web%>%html_nodes('.equip-list.fl')%>%html_attr('data-item') equip_list_f11x <- equip_list_f1[1]%>%str_split('\\|')%>%unlist()%>%as.numeric() equip_list_f11 <- sapply(equip_list_f11x,function(t) item$item_name[t==item$item_id]) #出装2 equip_list_f12x <- equip_list_f1[2]%>%str_split('\\|')%>%unlist()%>%as.numeric() equip_list_f12 <- sapply(equip_list_f12x,function(t) item$item_name[t==item$item_id]) re <- list(英雄名称=herolist$cname[id==herolist$ename], 英雄描述=base,组合=list(最佳搭档=cop1,压制英雄=cop2,被压制英雄=cop3), 建议铭文=sugg_ming,技能=skill_name,推荐召唤师=sugg_name3, 出装=list(方案1=equip_list_f11,方案2=equip_list_f12)) return(re) } dt <- lapply(herolist$ename, scrapy_fun) names(dt)=herolist$cname str(dt)
用R语言实现爬取王者荣耀数据
最后更新 2018-04-05 08:00 星期四 所属:
其他教程 浏览:575