1. 程式人生 > >【爬蟲相關】爬蟲爬取拉勾網的安卓招聘資訊

【爬蟲相關】爬蟲爬取拉勾網的安卓招聘資訊

我爬取了30頁拉勾上安卓的招聘資料告訴你 安卓崗位究竟要一個什麼樣的人

疑問

我知道沒圖你們是不會看的

大笑


如圖:以上是抓取了30頁拉勾上關於招聘安卓相關的內容 然後根據詞頻 製作出詞雲圖  出現最多的詞是 開發經驗

整體流程總共分為2步

1.爬蟲爬取相關的招聘資訊

2.根據獲取到的招聘資訊 生成詞雲圖

這裡的爬蟲採用的是scrapy框架  編輯器使用的是PyCharm,本次不是針對零基礎,如果對爬蟲感興趣推薦大家看這本 我就是看的這本書

先對拉勾網的資料進行分析 發現其中連結的規律:

變化的是2 也就是頁數


隨便點進去一個條目 又發現了這樣的規律


找到規律後 核心程式碼也就是2個正則表示式:

class LagouspiderSpider(scrapy.Spider):
    name = 'lagouspider'
    allowed_domains = ['www.lagou.com']
    start_urls = ['http://www.lagou.com/']

    def start_requests(self):
        for i in range(2,30):
            yield scrapy.Request(url='https://www.lagou.com/zhaopin/Android/'+str(i)+'/?filterOption=3')

    def parse(self, response):
        string = str(response.body)
        # print string
        pattern=r'https://www.lagou.com/jobs.\d*?.html'
        result=re.findall(pattern=pattern,string=string)
        for url in result:
            print url
            yield scrapy.Request(url=url,callback=self.parse_info)

    def parse_info(self,response):
        strZhiwei=response.css('.job_bt div').extract()[0]
        strZhiwei=strZhiwei.encode('utf-8')

        if os.path.exists('lagou.txt'):
            f=open('lagou.txt','ab')
            f.write(strZhiwei)
            f.close()
        else:
            f=open('lagou.txt','wb')
            f.write(strZhiwei)
            f.close()
然後把獲取到的資訊儲存到本地 格式是txt格式

然後下載第三方詞雲的庫 生成詞雲圖的核心程式碼

# -*- coding: utf-8 -*-
from os import path
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import jieba
import re
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator

d = path.dirname(__file__)

# Read the whole text.
text = open(path.join(d, 'lagou.txt')).read().decode('utf-8')
# read the mask / color image taken from
# hp://jirkavinse.deviantart.com/art/quot-Real-Life-quot-Alice-282261010
alice_coloring = np.array(Image.open(path.join(d, "a.png")))
stopwords = set(STOPWORDS)
stopwords.add("said")
# wc = WordCloud(font_path=font,background_color="white", max_words=2000, mask=alice_coloring,
#                stopwords=stopwords, max_font_size=40, random_state=42, )
# generate word cloud
wc = WordCloud(
                background_color = 'white',    # 設定背景顏色
                mask = alice_coloring,        # 設定背景圖片
                max_words = 7000,            # 設定最大顯示的字數
                font_path = 'AdobeHeitiStd-Regular.otf',# 設定字型格式,如不設定顯示不了中文
max_font_size = 95, # 設定字型最大值 random_state = 100, # 設定有多少種隨機生成狀態,即有多少種配色方案 ) def stop_words(texts): words_list = [] words_list = jieba.cut(texts, cut_all=False) # 返回的是一個迭代器 return ' '.join(words_list) # 注意是空格 text = stop_words(text) wc.generate(text) # create coloring from image image_colors = ImageColorGenerator(alice_coloring) # show plt.imshow(wc, interpolation="bilinear") plt.axis("off") plt.figure() # recolor wordcloud and show # we could also give color_func=image_colors directly in the constructor plt.imshow(wc.recolor(color_func=image_colors), interpolation="bilinear") plt.axis("off") plt.figure() plt.imshow(alice_coloring, cmap=plt.cm.gray, interpolation="bilinear") plt.axis("off") plt.show() wc.to_file("zq——zjl.jpg")

當然 同樣找規律 更改相關的連結可以爬取其他崗位 的招聘資訊,或者重寫正則表示式 獲取其他自己需要的內容

注意:如果請求的次數過多 拉勾 會有反爬蟲的相關措施,比如封禁ip

解決方案會在下次更新

程式碼下載連結