我们分别通过Golang、Python、Java三门语言,分别实现对Boss直聘网站的招聘数据进行爬取。
首先打开Boss直聘网站:
然后我们在职位类型中输入Go或者Golang关键字:
然后我们可以看到一个列表,和Go语言相关的各种招聘职位,还可以不停的下一页。。
那我们现在就来爬取这些数据:我们比较关心这里的职位名称,薪资待遇,工作地点,对于工作经验的要求,学历的要求,公司名称,公司类型,公司发展阶段,公司规模等等。。
一、分析页面
我们通过分析页面的结构发现,页面的职位列表,其实都位于一个ul中的li里,每个页面有30个职位,所以有30个li标签:
打开这个li标签后,里面是div标签嵌套,包括了招聘信息和公司信息:
接下来我们就可以通过代码来爬取这些数据了,首先我们要确定要爬取的第一个url:
https://www.zhipin.com/c101010100/?query=Go&page=1
一共有10页数据,分别通过page=1、2、3。。。来实现,
所以接下来要爬取的url:
https://www.zhipin.com/c101010100/?query=Go&page=2
https://www.zhipin.com/c101010100/?query=Go&page=3
。。。
https://www.zhipin.com/c101010100/?query=Go&page=10
爬取到的数据,我们也不需要处理,打印输出即可。。因为我们只是想看一下几门语言爬取数据在实现上有什么不同。。
好了,现在让我们来开开心心的撸代码吧。。
二、Golang语言实现
使用Go语言来爬取这个页面,github里搜了下,发现goquery这个爬虫包用的人还挺多的,7000多个star,而且是BSD开源协议,于是毫不犹豫的拿来用了。
goquery的使用还是比较简单,按照文档说明一步一步来就可以了:
首先:需要安装
localhost:~ ruby$ go get github.com/PuerkitoBio/goquery
其次:就是去看看goquery的API,先了解一下常用的方法:
https://godoc.org/github.com/PuerkitoBio/goquery
然后就可以开始写代码了:
打开Goland,新建一个go文件:
package main
import (
"github.com/PuerkitoBio/goquery"
"log"
"fmt"
"strconv"
"time"
)
func main() {
url := "https://www.zhipin.com/c101010100/?query=Go&page="
t := time.Now()
fmt.Println("============== 千锋教育Go语言开发教学部 职位信息分析 ================")
for offset := 0; offset < 10; offset++ {
time.Sleep(1 * time.Second)
doc, err := goquery.NewDocument(url + strconv.Itoa(offset))
handleErr(err)
fmt.Printf("第 %d 页的数据:\n", offset)
doc.Find(".job-primary").Each(func(i int, selection *goquery.Selection) {
item := Item{}
fmt.Printf("职位序号:第%d个职位\n", (i + 1))
item.position_name = selection.Find("div .job-title").Text()
fmt.Printf("职位名称:%s\n", item.position_name)
item.position_salary = selection.Find("div .red").Text()
fmt.Printf("职位薪酬:%s\n", item.position_salary)
item.work_address = selection.Find(".info-primary p").Children().Nodes[0].PrevSibling.Data
fmt.Printf("工作地点:%s\n", item.work_address)
item.work_experience = selection.Find(".info-primary p").Children().Nodes[0].NextSibling.Data
fmt.Printf("职位所需工作经历:%s\n", item.work_experience)
item.education = selection.Find(".info-primary p").Children().Nodes[1].NextSibling.Data
fmt.Printf("学历要求:%s\n", item.education)
item.company_name = selection.Find(".company-text .name").Children().First().Text()
fmt.Printf("公司名称:%s\n", item.position_name)
item.company_type = selection.Find(".company-text p").Children().Nodes[0].PrevSibling.Data
fmt.Printf("公司类型:%s\n", item.company_type )
if selection.Find(".company-text p").Children().Size() == 2 {
item.company_development_stage = selection.Find(".company-text p").Children().Nodes[0].NextSibling.Data
fmt.Printf("公司发展阶段:%s\n", item.company_deve