温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

R语言爬虫如何爬取招聘网站的招聘信息

发布时间:2021-11-22 11:04:21 来源:亿速云 阅读:144 作者:柒染 栏目:云计算

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)

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

关于R语言爬虫如何爬取招聘网站的招聘信息问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI