R语言爬虫爬取招聘网招聘信息

R语言爬取招聘网上的招聘信息,虽然R做爬虫确实没python专业,但是有一个包rvest我觉得还不错,我尝试爬取58同城招聘网上的数据。

rvest包,用到的函数有:

read_html(),

html_nodes(),

html_text(),

html_attr();

具体源代码如下:

#####加载相关包############

library(xml2)

library(rvest)

library(base)

下面获得每个子网站的地址

#58同城主网站网址

url<-"http://jingzhou.58.com/?utm_source=market&spm=b-31580022738699-pe-f-829.hao360_101"

web<-read_html(url,encoding = "utf-8")

xpath<-"div.col3 a"

#############子网站网址###########

title<-web%>%html_nodes(xpath)%>%html_text()

link<-web%>%html_nodes(xpath)%>%html_attr("href")

##########对子网站翻页########

id<-c(1:20)

pn<-paste0("pn",id,"/")#下载的网址有规律地缺失paste0()是字符串连接函数

###########清理网址中的缺失值#########

for (http in 1:length(link)) {

  httplink<-substr(link[http],1,4)

  if(httplink=="http"){link[http]<-substr(link[http],"",link[http])}

}

link<-na.omit(link)

filename<-"E:\\工作簿1.csv"#本地文件路径,用以存储下载的数据

job<-"职位";company<-"公司";location<-"地域";time<-"发布时间"

data<-data.frame(job,company,location,time)

len<-max(length(job),length(company),length(location),length(time))

########从子网站爬取数据##########

for (i in 1:length(link)) {

  link0<-paste0("http://jingzhou.58.com",link[i],pn,"&utm_source=market&spm=b-31580022738699-pe-f-829.hao360_101&PGTID=0d100000-00d9-7474-25a7-e93482a12861&ClickID=xxxx")

  link1<-paste0("http://jingzhou.58.com",link[i],pn,"?utm_source=market&spm=b-31580022738699-pe-f-829.hao360_101&PGTID=0d100000-00d9-7fce-21b0-b8956617e76f&ClickID=xxxx")

#####网址最后的变化###########

   for (j in 1:length(link)) {

    link0<-gsub("xxxx",j,link0)

    link1<-gsub("xxxx",j,link1)

#####网站名的两种类型用trycatch()消除错误#####

    for (k in 1:length(pn)) {

    tryCatch(

      {web<-read_html(link0[k],encoding = "utf-8")},

      error={web<-read_html(link1[k],encoding = "utf-8")})

###########提取需要的变量###########

  job_path<-"dt a"; #获取职位节点

  company_path<-"dd.w271 a";#获取公司节点

  location_path<-"dd.w96";#获取地域节点

  time_path<-"dd.w68";#获取发布时间节点

  job<-web%>%html_nodes(job_path)%>%html_text();

  company<-na.omit(web%>%html_nodes(company_path)%>%html_text());

  location<-web%>%html_nodes(location_path)%>%html_text();

  time<-web%>%html_nodes(time_path)%>%html_text();

  job<-job[1:len];company<-company[1:len];location<-location[1:len];time<-time[1:len]#长度一致#

  data1<-data.frame(job,company,location,time);

 if(length(data>0)){

  data<-na.omit(rbind(data,data1))}

  else

  {data1<-rep(NA,len);data<-na.omit(rbind(data,data1))}

    }

  }

  Sys.sleep(3)#每隔3秒钟停一次,防止反爬虫

  print(i)

  print("sleep end,download start!")

}

data<-data[-1,]

write.csv(data,file = filename)

就是这样了,自己实操一遍才好。

个人博客网站:http://www.cj318.cn/

点击【阅读原文】,买课程,送U盘!

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大数据技术派

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值