您好,登錄后才能下訂單哦!
R語(yǔ)言爬蟲(chóng)如何爬取招聘網(wǎng)站的招聘信息,針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
R語(yǔ)言爬取招聘網(wǎng)上的招聘信息,雖然R做爬蟲(chóng)確實(shí)沒(méi)python專業(yè),但是有一個(gè)包rvest我覺(jué)得還不錯(cuò),我嘗試爬取58同城招聘網(wǎng)上的數(shù)據(jù)。
rvest包,用到的函數(shù)有:
read_html(),
html_nodes(),
html_text(),
html_attr();
具體源代碼如下:
#####加載相關(guān)包############
library(xml2)
library(rvest)
library(base)
下面獲得每個(gè)子網(wǎng)站的地址
#58同城主網(wǎng)站網(wǎng)址
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"
#############子網(wǎng)站網(wǎng)址###########
title<-web%>%html_nodes(xpath)%>%html_text()
link<-web%>%html_nodes(xpath)%>%html_attr("href")
##########對(duì)子網(wǎng)站翻頁(yè)########
id<-c(1:20)
pn<-paste0("pn",id,"/")#下載的網(wǎng)址有規(guī)律地缺失paste0()是字符串連接函數(shù)
###########清理網(wǎng)址中的缺失值#########
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"#本地文件路徑,用以存儲(chǔ)下載的數(shù)據(jù)
job<-"職位";company<-"公司";location<-"地域";time<-"發(fā)布時(shí)間"
data<-data.frame(job,company,location,time)
len<-max(length(job),length(company),length(location),length(time))
########從子網(wǎng)站爬取數(shù)據(jù)##########
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")
#####網(wǎng)址最后的變化###########
for (j in 1:length(link)) {
link0<-gsub("xxxx",j,link0)
link1<-gsub("xxxx",j,link1)
#####網(wǎng)站名的兩種類型用trycatch()消除錯(cuò)誤#####
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"; #獲取職位節(jié)點(diǎn)
company_path<-"dd.w271 a";#獲取公司節(jié)點(diǎn)
location_path<-"dd.w96";#獲取地域節(jié)點(diǎn)
time_path<-"dd.w68";#獲取發(fā)布時(shí)間節(jié)點(diǎn)
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]#長(zhǎng)度一致#
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秒鐘停一次,防止反爬蟲(chóng)
print(i)
print("sleep end,download start!")
}
data<-data[-1,]
write.csv(data,file = filename)
就是這樣了,自己實(shí)操一遍才好。
關(guān)于R語(yǔ)言爬蟲(chóng)如何爬取招聘網(wǎng)站的招聘信息問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。