前言
利用python爬取在前程無憂網搜索python關鍵字出現的最新的招聘資料,保存到本地Excel,進行資料查看和預處理,然后利用matplotlib進行資料分析和可視化,
1. 爬取資料
目標url:
https://www.51job.com/
在前程無憂網輸入關鍵字python,搜索有關的崗位資料,翻頁查看這些招聘崗位資訊,可以發現url翻頁的規律,
檢查網頁源代碼,可以找到想要提取的資料,
部分爬蟲代碼如下,完整見文末下載
async def parse(self, text):
# 正則匹配提取資料 try:
job_name = re.findall('"job_name":"(.*?)",', text) # 職位
company_name = re.findall('"company_name":"(.*?)",', text) # 公司名稱
salary = re.findall('"providesalary_text":"(.*?)",', text)
salary = [i.replace('\\', '') for i in salary] # 薪酬 去掉 \ 符號
city = re.findall('"workarea_text":"(.*?)",', text) # 城市
job_welfare = re.findall('"jobwelf":"(.*?)",', text) # 職位福利
attribute_text = re.findall('"attribute_text":(.*?),"companysize_text"', text)
attribute_text = ['|'.join(eval(i)) for i in attribute_text]
companysize = re.findall('"companysize_text":"(.*?)",', text) # 公司規模
category = re.findall('"companyind_text":"(.*?)",', text)
category = [i.replace('\\', '') for i in category] # 公司所屬行業 去掉 \ 符號
datas = pd.DataFrame({'company_name': company_name, 'job_name': job_name, 'companysize': companysize, 'city': city, 'salary': salary, 'attribute_text': attribute_text, 'category': category, 'job_welfare': job_welfare})
datas.to_csv('job_info.csv', mode='a+', index=False, header=True)
logging.info({'company_name': company_name, 'job_name': job_name, 'company_size': companysize, 'city': city, 'salary': salary, 'attribute_text': attribute_text, 'category': category, 'job_welfare': job_welfare})
except Exception as e:
print(e)
運行效果如下:
爬取了200頁的招聘資料,共10000條招聘資訊,用時49.919s,
2. 資料查看和預處理
import pandas as pd
df = pd.read_csv('job_info.csv')
# 異步爬蟲爬取資料時 datas.to_csv('job_info.csv', mode='a+', index=False, header=True) 洗掉多的列名
df1 = df[df['salary'] != 'salary']
# 查看前10行
df1.head(10)
# city那一列資料 處理為城市
# 按 - 分割 expand=True 0那一列重新賦值給df['city']
df1['city'] = df1['city'].str.split('-', expand=True)[0]
df1.head(10)
# 經驗要求 學歷要求 有的話是在attribute_text列里
df['attribute_text'].str.split('|', expand=True)
df1['experience'] = df1['attribute_text'].str.split('|', expand=True)[1]
df1['education'] = df1['attribute_text'].str.split('|', expand=True)[2]
df1
保存為已清洗資料
df1.to_csv('已清洗資料.csv', index=False)
查看索引、資料型別和記憶體資訊
df2 = pd.read_csv('已清洗資料.csv')
df2.info()
3. 資料分析與可視化
(1) 柱形圖展示招聘崗位數最多的城市Top10
代碼如下:
import pandas as pd
import randomimport matplotlib.pyplot as plt
import matplotlib as mpl
df = pd.read_csv('已清洗資料.csv')
# 有些是異地招聘 過濾掉data = df[df['city'] != '異地招聘']['city'].value_counts()
city = list(data.index)[:10] # 城市
nums = list(data.values)[:10] # 崗位數
print(city)print(nums)colors = ['#FF0000', '#0000CD', '#00BFFF', '#008000', '#FF1493', '#FFD700', '#FF4500', '#00FA9A', '#191970', '#9932CC']
random.shuffle(colors)# 設定大小 像素plt.figure(figsize=(9, 6), dpi=100)
# 設定中文顯示mpl.rcParams['font.family'] = 'SimHei'
# 繪制柱形圖 設定柱條的寬度和顏色# color引數 每根柱條配置不同顏色plt.bar(city, nums, width=0.5, color=colors)
# 添加描述資訊plt.title('招聘崗位數最多的城市Top10', fontsize=16)
plt.xlabel('城市', fontsize=12)
plt.ylabel('崗位數', fontsize=12)
# 展示圖片plt.show()
運行效果如下:
['上海', '深圳', '廣州', '北京', '杭州', '成都', '武漢', '南京', '蘇州', '長沙']
[2015, 1359, 999, 674, 550, 466, 457, 444, 320, 211]
上海、深圳、廣州、北京提供了很多崗位,杭州、成都、武漢、南京等城市的招聘崗位數量也比較可觀,
(2) 計算崗位資料的薪酬,處理為多少K/月,劃分薪酬區間,統計薪酬分布情況,餅圖展示,
代碼如下:
# 設定中文顯示
mpl.rcParams['font.family'] = 'SimHei'
# 設定大小 像素plt.figure(figsize=(9, 6), dpi=100)
plt.axes(aspect='equal') # 保證餅圖是個正圓
explodes = [0, 0, 0, 0.1, 0.2, 0.3, 0.4, 0.5]
plt.pie(nums, pctdistance=0.75, shadow=True,
colors=colors, autopct='%.2f%%', explode=explodes,
startangle=15, labeldistance=1.1,
)# 設定圖例 調節圖例位置plt.legend(part_interval, bbox_to_anchor=(1.0, 1.0))
plt.title('招聘崗位的薪酬分布', fontsize=15)
plt.show()
運行效果如下:
招聘崗位給的薪酬在5K-10K和10K-15K區間所占的比例較大,也有一定比例的50K以上的高薪資崗位,
(3) 查看招聘崗位對學歷的要求的情況,水平柱形圖可視化,
mport pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
df = pd.read_csv(r'已清洗資料.csv')['education']
data = df.value_counts()labels = ['大專', '本科', '碩士', '博士']
nums = [data[i] for i in labels]
print(labels)print(nums)colors = ['cyan', 'red', 'yellow', 'blue']
# 設定中文顯示
mpl.rcParams['font.family'] = 'SimHei'
# 設定顯示風格
plt.style.use('ggplot')
# 設定大小 像素
plt.figure(figsize=(8, 6), dpi=100)
# 繪制水平柱狀圖
plt.barh(labels, nums, height=0.36, color=colors)
plt.title('招聘崗位對學歷的要求', fontsize=16)
plt.xlabel('崗位數量', fontsize=12)
plt.show()
運行效果如下:
['大專', '本科', '碩士', '博士']
[2052, 6513, 761, 45]
(4) 查看招聘崗位對作業經驗的要求的情況,水平柱形圖可視化,
由于得到的作業經驗列里的資料并不規范,統計時需做特殊處理
代碼如下:
# 設定中文顯示
mpl.rcParams['font.family'] = 'SimHei'
# 設定顯示風格plt.style.use('ggplot')
# 設定大小 像素plt.figure(figsize=(9, 6), dpi=100)
# 繪制水平柱狀圖plt.barh(labels, nums, height=0.5, color=colors)
plt.title('招聘崗位對作業經驗的要求', fontsize=16)
plt.xlabel('崗位數量', fontsize=12)
plt.show()
運行效果如下:
3-4年經驗 3361
2年經驗 2114
1年經驗 1471
5-7年經驗 1338
在校生\/應屆生 661
無需經驗 417
本科 182
8-9年經驗 105
10年以上經驗 64
碩士 59
招1人 57
招若干人 57
招2人 42
大專 30
招3人 14
博士 11
招5人 9
招4人 5
招10人 2
招7人 1
Name: experience, dtype: int64
['無需經驗', '1年經驗', '2年經驗', '3-4年經驗', '5-7年經驗', '8-9年經驗', '10年以上經驗']
[1260, 1530, 2114, 3372, 1338, 105, 64]
【】#### (5) 查看招聘公司所屬行業的分布情況,詞云展示,
代碼如下:
import pandas as pd
import collectionsfrom wordcloud import WordCloud
import matplotlib.pyplot as plt
df = pd.read_csv(r'已清洗資料.csv')['category']
data = list(df.values)word_list = []for i in data:
x = i.split('/')
for j in x:
word_list.append(j)word_counts = collections.Counter(word_list)# 繪制詞云
my_cloud = WordCloud(
background_color='white', # 設定背景顏色 默認是black
width=900, height=500,
font_path='simhei.ttf', # 設定字體 顯示中文
max_font_size=120, # 設定字體最大值
min_font_size=15, # 設定子圖最小值
random_state=60 # 設定隨機生成狀態,即多少種配色方案
).generate_from_frequencies(word_counts)
# 顯示生成的詞云圖片
plt.imshow(my_cloud, interpolation='bilinear')
# 顯示設定詞云圖中無坐標軸
plt.axis('off')
plt.show()
運行效果如下:
(6) 查看招聘崗位的職位福利,詞云展示,
代碼與上文一致
運行效果如下:
職位福利關鍵詞中出現頻率較高的有五險一金、年終獎金、績效獎金、定期體檢、餐飲補貼等,
本文的文字及圖片來源于網路,僅供學習、交流使用,不具有任何商業用途,著作權歸原作者所有,如有問題請及時聯系我們以作處理,
舊時晚風拂曉城 | 作者
凹凸資料 | 來源
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/5067.html
標籤:Python
