- #install.packages("D:/software/R/library/Rwebdriver-master",repos=NULL,type="source")
- #在cmd中D:\D盘存软件\firefox
- #java -jar selenium-server-standalone-3.141.59.jar
- library(RCurl)# 抓取数据
- library(XML)# 解析网页
- library(stringr)# 字符串处理
- library(dplyr)# 调用%>%管道
- library(Rwebdriver)# 爬取动态网页
- library(devtools)
- start_session(root = 'http://localhost:4444/wd/hub/',browser ="firefox")# 默认端口是4444,我的浏览器是chorme,如果使用火狐改成firefox
- list_url <- "https://www.lagou.com/jobs/list_%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90"
- post.url(url = list_url)# 打开网页
- pageSource <- page_source()#保存网页信息
- list_web <- htmlParse(pageSource,encoding = "UTF-8")
- namelist = list() ##创建存贮的变量
- #循环下载10页数据
- for (i in 1:10){
- title <- list_web %>% getNodeSet("//h3")%>%sapply(xmlValue) #用的是xpath代码
- area <- list_web%>% getNodeSet("//*[(@id = 's_position_list')]//em")%>%sapply(xmlValue)
- namelist[[i]] = cbind(title,area)#结果的保存
- button <- element_xpath_find(value = "//*[@id='order']/li/div[4]/div[2]")# 获取下一页按钮位置
- element_click(ID=button) #点击下一页
- print(paste("第",i,"页数据爬取完成",sep=""))
- Sys.sleep(5)#睡眠5秒
- }
- result_data = noquote(do.call(rbind,namelist))
- result_data = data.frame(岗位=result_data[,1],地址=result_data[,2])
- result_data


雷达卡






京公网安备 11010802022788号







