python爬取前程無憂分析一波機械職位資訊
秋招這個時候已經開始了,正所謂知己知彼方能百戰不殆,今天就從招聘網站下手分析一波機械的就業前景。
這次獲取資料的網站是前程無憂,之所以選擇前程無憂,是因為資料獲取沒有什麼難度,再者前程無憂提供的崗位資訊比智聯招聘,拉勾提供的資料都多。
看了一下,前程無憂提供了 2000頁/50條 資料,考慮到資料可能有些重複,這裡獲取 48569 條資料。
資料獲取
- 用到的爬蟲框架是 scrapy
- 解析庫 xpath、re、pymongo
- 儲存資料用 mongodb
- 資料處理用 pyecharts。
開啟 https://www.51job.com/ 在搜尋框輸入機械,跳轉到的網頁是這個樣子的:

這裡面的欄位還不足以滿足我們的需求,還想獲取職位描述,和公司的型別,例如國企還是民營等這些資料。於是我們點開看看每一條資料的欄位。

具體欄位
框起來的就是我們要獲取的內容。
接下來上程式碼,使用scrapy 提供的預設模板建立爬蟲專案。
class A51jobSpider(scrapy.Spider): name = '51job' allowed_domains = ['51job.com'] keyword = quote('機械') headers = { 'Host': 'search.51job.com', 'Referer': 'https://www.51job.com/', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36' } def start_requests(self): '''獲取開始抓取的頁面''' for i in range(1,1000): url = 'https://search.51job.com/list/000000,000000,0000,00,9,99,'+ self.keyword + ',2,{}.html'.format(str(i)) yield scrapy.Request(url=url,headers=self.headers,callback=self.parse_detial_url) def parse_detial_url(self,response): '''獲取詳情頁的url''' s = etree.HTML(response.text) detail_urls = s.xpath('//*[@id="resultList"]/div/p/span/a/@href') for detial_url in detail_urls: url = detial_url yield scrapy.Request(url=url,headers=self.headers,callback=self.parse)
這裡通過重寫 start_request 獲取詳情資訊圖片的連結,接下來解析具體欄位資料:
def parse(self, response): '''解析詳情頁具體欄位''' item = JixieItem() # 例項化類 s = etree.HTML(response.text) jobs = s.xpath('//div[@class="tHeader tHjob"]/div/div[1]/h1/text()') if jobs: item['job'] = jobs[0].strip() else: item['job'] = '' companys = s.xpath('//div[@class="tHeader tHjob"]/div/div[1]/p[1]/a[1]/text()') if companys: item['company'] = companys[0].strip() else: item['company'] = '' company_types = s.xpath('//div[@class="com_tag"]/p/text()') if company_types: item['company_type'] = company_types[0] else: item['company_type'] = '' data = s.xpath('//div[@class="tHeader tHjob"]/div/div[1]/p[2]/text()') if data: item['area'] = data[0].strip() item['experience'] = data[1].strip() item['degree'] = data[2].strip() salarys = s.xpath('//div[@class="tHeader tHjob"]/div/div[1]/strong/text()') if salarys: item['salary'] = salarys[0].strip() else: item['salary'] = '' describes = re.findall(re.compile('<div class="bmsg job_msg inbox">(.*?)div class="mt10"', re.S), response.text) if describes: item['describe'] = describes[0].strip().replace('<p>', '').replace('</p>','').replace('<p>','').replace('<span>','').replace('</span>','').replace('\t','') yield item
items.py 檔案了定義了具體的欄位。items 是儲存爬取資料的容器,使用方法和字典差不多。不過,相比字典,item 多了額外的保護機制,可以避免拼寫錯誤或者定義欄位的錯誤。
class JixieItem(scrapy.Item): # 定義抓取的欄位 job = Field() company = Field() company_type = Field() area = Field() experience = Field() degree = Field() salary = Field() describe = Field()
接下來把資料儲存到 MongoDB 中,這裡的程式碼格式是一樣的,幾乎不需要修改就可以從 scrapy 的文件中拿過來用:
class MongoPipeline(object): def __init__(self,mongo_uri,mongo_db): self.mongo_uri = mongo_uri self.mongo_db = mongo_db @classmethod def from_crawler(cls,crawler): return cls( mongo_uri = crawler.settings.get('MONGO_URI'), mongo_db = crawler.settings.get('MONGO_DB') ) def open_spider(self,spider): self.client = pymongo.MongoClient(self.mongo_uri) self.db = self.client[self.mongo_db] def process_item(self,item,spider): name = item.__class__.__name__ self.db[name].insert(dict(item)) return item def close_spider(self,spider): self.client.close()
資料獲取成功,看一下資料:

資料分析
這一部分才是重頭戲
先看一下學歷佔比:

學歷佔比
可以看到碩士需求不多,並不是說需求不多,而是在大的環境下,相對來說少。這裡的大環境是指據統計全國本科及以上學歷比例不到 10%
細心的小夥伴在求職時可以發現雖說公司打著學歷要求不高,可任職要求已經完全超出了對學歷的要求,所以能提高學歷盡力提高學歷,提高競爭力。
再來看一下工作經驗在機械中的要求:

工作經驗
可以看到機械相關職位在招聘時對工作經驗的要求佔了很大的一部分,這對應屆畢業生求職來說可是個好訊息。同時看到五年以後的需求沒有那麼大了,這是招聘公司不需要工作經驗久的員工了嗎?
猜測
1、對於機械行業來說,公司福利待遇基本上一樣,求職者在五六年經驗後,基本生活工作穩定,不會再想著跳槽從新開始。
2、現在招聘旺季是給應屆畢業生。

公司型別
雖說民營公司佔了大部分,但是能去國企還是儘量去國企。在同等情況下上市公司和有融資的公司都是不錯的選擇(工資高,福利好),當然在你拿到對應 offer 再說吧。

機械詞雲圖.jpg
這裡是根據職位描述生成的詞雲圖,由於資料量比較大,這張圖的參考價值不是很大,其實是想獲取哪些具體的專業技能要求最多,但還是看出繪圖軟體需求高一些。同時不能忽略軟實力的重要,辦公軟體也是要熟練掌握的。
總結
綜上所述,機械的就業前景還是挺不錯的,同時自身實力過硬也是必須的。沒事的時候看看招聘網站上的任職要求和自身實力匹配一下,補充一下自身實力,在招聘的時候才能有所進退。
最後點贊是一種態度。