根據(jù)我們對(duì)當(dāng)個(gè)招聘崗位的分析,我們發(fā)現(xiàn)我們需要爬取的數(shù)據(jù)主要有八個(gè)冠王,分別是:職位月薪赶撰、工作地點(diǎn)、發(fā)布日期柱彻、工作性質(zhì)豪娜、工作經(jīng)驗(yàn)、最低學(xué)歷哟楷、招聘人數(shù)瘤载、職位類(lèi)別。
所以我們首先在Items.py中定義我們要抓取的數(shù)據(jù)卖擅,如下所示:
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
數(shù)據(jù)類(lèi)型定義好之后鸣奔,我們開(kāi)始編寫(xiě)爬蟲(chóng)的主體部分,打開(kāi)my_spider.py惩阶,其中已經(jīng)自動(dòng)為我們生成了不少代碼挎狸,其中包括def parse(self,response):
方法。由于我們要抓取多頁(yè)頁(yè)面断楷,因此我們?cè)谧远x一個(gè)def parse_item(self,response):
方法锨匆。這樣,我們使用def parse_item(self,response):
來(lái)抓取某一具體頁(yè)面的內(nèi)容冬筒,然后使用def parse(self,response):
方法來(lái)循環(huán)調(diào)用def parse_item(self,response):
恐锣。
(1)首先我們編寫(xiě)def parse_item(self,response):
方法:
其中theitem 用來(lái)存儲(chǔ)抓取的數(shù)據(jù)并返回,a1定位到了class="terminalpage-left"
的div,然后根據(jù)我們對(duì)網(wǎng)頁(yè)的分析舞痰,提取出具體數(shù)據(jù)到item中土榴,并存到theitem返回。這里大量用到了xpath,不會(huì)的同學(xué)可以從這個(gè)網(wǎng)站簡(jiǎn)單學(xué)習(xí)一下:http://www.w3school.com.cn/xpath/xpath_syntax.asp
#爬取具體的內(nèi)容
def parse_item(self,response):
theitems =[]
item = NewoneItem()
a1 = response.xpath('//div[@class="terminalpage-left"]')
#為了處理掉出現(xiàn)的空格匀奏,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):
循環(huán)調(diào)用上述方法鞭衩。需要注意的是学搜,我們需要給出start_urls,鏈接地址即為搜索結(jié)果地址论衍。href中是我們從網(wǎng)頁(yè)中找到本頁(yè)所有的工作鏈接地址瑞佩。因此我們使用一個(gè)for循環(huán)來(lái)遍歷這些地址,使用yield scrapy.Request(he,callback = self.parse_item)
打開(kāi)這個(gè)地址坯台,并調(diào)用我們編寫(xiě)的self.parse_item方法炬丸,提取當(dāng)前這個(gè)崗位的信息,如此循環(huán)直到本頁(yè)面中所有的鏈接都被爬取過(guò)蜒蕾。爬取完當(dāng)前這一頁(yè)的內(nèi)容之后稠炬,我們需要轉(zhuǎn)到下一頁(yè),我們注意到下一頁(yè)的鏈接地址是在頁(yè)面的下一頁(yè)按鈕中咪啡,因此下一頁(yè)的地址就是thenexthref= response.xpath('//div[@class="newl'+'ist_wrap fl"]/div[@class="pages'+'Down"]/ul/li[@class="pagesDow'+'n-pos"]/a/@href').extract()
首启,大家可以自行嘗試分析一下,找到下一頁(yè)地址后撤摸,我們將地址加到start_urls中毅桃,這樣又可以繼續(xù)抓取。
# -*- 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准夷。循環(huán)爬取
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)
#爬取下一頁(yè)
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)
如果我們需要保存抓取的數(shù)據(jù)钥飞,我們首先要進(jìn)行相關(guān)設(shè)置, 在setting.py中將下面的代碼取消注釋衫嵌。
# Configure item pipelines
# See http://scrapy.readthedocs.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
'newone.pipelines.NewonePipeline': 300,
}
然后在piplines.py中編寫(xiě)保存數(shù)據(jù)的代碼读宙,我們將數(shù)據(jù)保存為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
做好這些后结闸,我們就可以執(zhí)行爬蟲(chóng)爬取命令了,在Anaconda Prompt進(jìn)入你項(xiàng)目的目錄酒朵,然后執(zhí)行
scrapy crawl my_spider
命令膀估,就可以看到數(shù)據(jù)的爬取過(guò)程,最終數(shù)據(jù)存到news.json文件中耻讽,如下圖所示:
本文參考了lucky_yang_的博客;另外本文代碼地址:鏈接: https://pan.baidu.com/s/1jn1aloADaqoH2Ra5343SHQ 密碼: 55bc