从零开始实现scrapy爬取智联招聘的岗位信息-3

根据我们对当个招聘岗位的分析,我们发现我们需要爬取的数据主要有八个,分别是:职位月薪、工作地点、发布日期、工作性质、工作经验、最低学历、招聘人数、职位类别。
所以我们首先在Items.py中定义我们要抓取的数据,如下所示:

import scrapy
class NewoneItem(scrapy.Item):
    # define the fields for your item here like:
    #myurl = scrapy.Field()
    salary = scrapy.Field()
    position = scrapy.Field()
    time = scrapy.Field()
    xingzhi = scrapy.Field()
    experience = scrapy.Field()
    education = scrapy.Field()
    number = scrapy.Field()   
    leibie = scrapy.Field()
    #pass

数据类型定义好之后,我们开始编写爬虫的主体部分,打开my_spider.py,其中已经自动为我们生成了不少代码,其中包括def parse(self,response):方法。由于我们要抓取多页页面,因此我们在自定义一个def parse_item(self,response):方法。这样,我们使用def parse_item(self,response):来抓取某一具体页面的内容,然后使用def parse(self,response):方法来循环调用def parse_item(self,response):
(1)首先我们编写def parse_item(self,response):方法:
其中theitem 用来存储抓取的数据并返回,a1定位到了class="terminalpage-left"的div,然后根据我们对网页的分析,提取出具体数据到item中,并存到theitem返回。这里大量用到了xpath,不会的同学可以从这个网站简单学习一下:http://www.w3school.com.cn/xpath/xpath_syntax.asp

 #爬取具体的内容
    def parse_item(self,response):
        theitems =[]
        item = NewoneItem()
        a1 = response.xpath('//div[@class="terminalpage-left"]')
#为了处理掉出现的空格,salary特殊处理
        temp1 = a1.xpath('./ul/li[1]/strong/text()').extract()[0]
        temp2 = ''
        for te in temp1:
            te = te.strip()
            if te!="":
                temp2 = temp2+te
        item['salary'] = temp2
        item['position'] = a1.xpath('./ul/li[2]/strong/a/text()').extract()
        item['time'] = a1.xpath('./ul/li[3]/strong/span/text()').extract()
        item['xingzhi'] = a1.xpath('./ul/li[4]/strong/text()').extract()
        item['experience'] = a1.xpath('./ul/li[5]/strong/text()').extract()
        item['education'] = a1.xpath('./ul/li[6]/strong/text()').extract()
        item['number'] = a1.xpath('./ul/li[7]/strong/text()').extract()
        item['leibie'] = a1.xpath('./ul/li[8]/strong/a/text()').extract()
        theitems.append(item)
        
        return theitems

(2)然后在def parse_item(self,response):循环调用上述方法。需要注意的是,我们需要给出start_urls,链接地址即为搜索结果地址。href中是我们从网页中找到本页所有的工作链接地址。因此我们使用一个for循环来遍历这些地址,使用yield scrapy.Request(he,callback = self.parse_item)打开这个地址,并调用我们编写的self.parse_item方法,提取当前这个岗位的信息,如此循环直到本页面中所有的链接都被爬取过。爬取完当前这一页的内容之后,我们需要转到下一页,我们注意到下一页的链接地址是在页面的下一页按钮中,因此下一页的地址就是thenexthref= response.xpath('//div[@class="newl'+'ist_wrap fl"]/div[@class="pages'+'Down"]/ul/li[@class="pagesDow'+'n-pos"]/a/@href').extract(),大家可以自行尝试分析一下,找到下一页地址后,我们将地址加到start_urls中,这样又可以继续抓取。

# -*- coding: utf-8 -*-
import scrapy
from newone.items import NewoneItem

class MySpiderSpider(scrapy.Spider):
    name = "my_spider"
    allowed_domains = ["zhaopin.com"]
    start_urls = ["https://sou.zhaopin.com/jobs/searchresult.ashx?jl=538&kw=it&sm=0&p=1"]
    
    #得到URL。循环爬取
    def parse(self, response):
            href = response.xpath('//td[@class="zwmc"]/div/a[1]/@href').extract()
            for he in href:
                yield scrapy.Request(he,callback = self.parse_item)
                
            #爬取下一页
            thenexthref= response.xpath('//div[@class="newl'+'ist_wrap fl"]/div[@class="pages'+'Down"]/ul/li[@class="pagesDow'+'n-pos"]/a/@href').extract()
            
            self.start_urls.append(thenexthref)
            if thenexthref:
                thenexthref=thenexthref[0]
                yield scrapy.Request(thenexthref,callback=self.parse)

如果我们需要保存抓取的数据,我们首先要进行相关设置, 在setting.py中将下面的代码取消注释。

# Configure item pipelines
# See http://scrapy.readthedocs.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
    'newone.pipelines.NewonePipeline': 300,
}

然后在piplines.py中编写保存数据的代码,我们将数据保存为json格式,命名为news,如下所示:

import codecs
import json
import os

class NewonePipeline(object):
    def process_item(self, item, spider):
        base_dir = os.getcwd()
        filename = base_dir + '/news.json'
        with codecs.open(filename, 'a') as f:
            line = json.dumps(dict(item), ensure_ascii=False) + '\n'
            f.write(line)
        return item

做好这些后,我们就可以执行爬虫爬取命令了,在Anaconda Prompt进入你项目的目录,然后执行
scrapy crawl my_spider命令,就可以看到数据的爬取过程,最终数据存到news.json文件中,如下图所示:

《从零开始实现scrapy爬取智联招聘的岗位信息-3》 333.png

本文参考了lucky_yang_的博客;另外本文代码地址:链接: https://pan.baidu.com/s/1jn1aloADaqoH2Ra5343SHQ 密码: 55bc

    原文作者:岁月淡如水
    原文地址: https://www.jianshu.com/p/1438164883ad
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞