全文簡(jiǎn)介
本文是先采集拉勾網(wǎng)上面的數(shù)據(jù),采集的是Python崗位的數(shù)據(jù),然后用Python進(jìn)行可視化。主要涉及的是爬蟲(chóng)&數(shù)據(jù)可視化的知識(shí)。
爬蟲(chóng)部分
先用Python來(lái)抓取拉勾網(wǎng)上面的數(shù)據(jù),采用的是簡(jiǎn)單好用的requests模塊。主要注意的地方是,拉勾網(wǎng)屬于動(dòng)態(tài)網(wǎng)頁(yè),所以會(huì)用到瀏覽器的F12開(kāi)發(fā)者工具進(jìn)行抓包。抓包以后會(huì)發(fā)現(xiàn),其實(shí)網(wǎng)頁(yè)是一個(gè)POST的形式,所以要提交數(shù)據(jù),提交的數(shù)據(jù)如下圖:
真實(shí)網(wǎng)址是:
https://www.lagou.com/jobs/positionAjax.jsonneedAddtionalResult=false&isSchoolJob=0
在上圖也可以輕松發(fā)現(xiàn):kd是查詢(xún)關(guān)鍵詞,pn是頁(yè)數(shù),可以實(shí)現(xiàn)翻頁(yè)。
代碼實(shí)現(xiàn)
import requests # 網(wǎng)絡(luò)請(qǐng)求
import re
import time
import random
# post的網(wǎng)址
url = 'https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false&isSchoolJob=0'
# 反爬措施
header = {'Host': 'www.lagou.com',
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Language': 'zh-CN,en-US;q=0.7,en;q=0.3',
'Accept-Encoding': 'gzip, deflate, br',
'Referer': 'https://www.lagou.com/jobs/list_Python?labelWords=&fromSearch=true&suginput=',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'X-Requested-With': 'XMLHttpRequest',
'X-Anit-Forge-Token': 'None',
'X-Anit-Forge-Code': '0',
'Content-Length': '26',
'Cookie': 'user_trace_token=20171103191801-9206e24f-9ca2-40ab-95a3-23947c0b972a; _ga=GA1.2.545192972.1509707889; LGUID=20171103191805-a9838dac-c088-11e7-9704-5254005c3644; JSESSIONID=ABAAABAACDBABJB2EE720304E451B2CEFA1723CE83F19CC; _gat=1; LGSID=20171228225143-9edb51dd-ebde-11e7-b670-525400f775ce; PRE_UTM=; PRE_HOST=www.baidu.com; PRE_SITE=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DKkJPgBHAnny1nUKaLpx2oDfUXv9ItIF3kBAWM2-fDNu%26ck%3D3065.1.126.376.140.374.139.129%26shh%3Dwww.baidu.com%26sht%3Dmonline_3_dg%26wd%3D%26eqid%3Db0ec59d100013c7f000000055a4504f6; PRE_LAND=https%3A%2F%2Fwww.lagou.com%2F; LGRID=20171228225224-b6cc7abd-ebde-11e7-9f67-5254005c3644; index_location_city=%E5%85%A8%E5%9B%BD; TG-TRACK-CODE=index_search; SEARCH_ID=3ec21cea985a4a5fa2ab279d868560c8',
'Connection': 'keep-alive',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache'}
for n in range(30):
# 要提交的數(shù)據(jù)
form = {'first':'false',
'kd':'Python',
'pn':str(n)}
time.sleep(random.randint(2,5))
# 提交數(shù)據(jù)
html = requests.post(url,data=form,headers = header)
# 提取數(shù)據(jù)
data = re.findall('{'companyId':.*?,'positionName':'(.*?)','workYear':'(.*?)','education':'(.*?)','jobNature':'(.*?)','financeStage':'(.*?)','companyLogo':'.*?','industryField':'.*?','city':'(.*?)','salary':'(.*?)','positionId':.*?,'positionAdvantage':'(.*?)','companyShortName':'(.*?)','district'',html.text)
# 轉(zhuǎn)換成數(shù)據(jù)框
data = pd.DataFrame(data)
# 保存在本地
data.to_csv(r'D:\Windows 7 Documents\Desktop\My\LaGouDataMatlab.csv',header = False, index = False, mode = 'a+')
注意:抓取數(shù)據(jù)的時(shí)候不要爬取太快,除非你有其他的反爬措施,比如更換IP等,另外不需登錄,我在代碼加入了time模塊,用于限制爬取速度。
數(shù)據(jù)可視化
下載下來(lái)的數(shù)據(jù)長(zhǎng)成這個(gè)樣子:
注意標(biāo)題(也就是列明)是我自己添加的。
導(dǎo)入模塊并配置繪圖風(fēng)格
import pandas as pd # 數(shù)據(jù)框操作
import numpy as np
import matplotlib.pyplot as plt # 繪圖
import jieba # 分詞
from wordcloud import WordCloud # 詞云可視化
import matplotlib as mpl # 配置字體
from pyecharts import Geo # 地理圖
mpl.rcParams['font.sans-serif'] = ['Microsoft YaHei']
# 配置繪圖風(fēng)格
plt.rcParams['axes.labelsize'] = 16.
plt.rcParams['xtick.labelsize'] = 14.
plt.rcParams['ytick.labelsize'] = 14.
plt.rcParams['legend.fontsize'] = 12.
plt.rcParams['figure.figsize'] = [15., 15.]
注意:導(dǎo)入模塊的時(shí)候其他都容易解決,除了wordcloud這個(gè)模塊,這個(gè)模塊我建議大家手動(dòng)安裝,如果pip安裝的話,會(huì)提示你缺少C++14.0之類(lèi)的錯(cuò)誤,導(dǎo)致安裝不上。手動(dòng)下載whl文件就可以順利安裝了。
數(shù)據(jù)預(yù)覽
# 導(dǎo)入數(shù)據(jù)
data = pd.read_csv('D:\\Windows 7 Documents\\Desktop\\My\\LaGouDataPython.csv',encoding='gbk') # 導(dǎo)入數(shù)據(jù)
data.head()
read_csv路徑不要帶有中文
data.tail()
學(xué)歷要求
data['學(xué)歷要求'].value_counts().plot(kind='barh',rot=0)
plt.show()
工作經(jīng)驗(yàn)
data['工作經(jīng)驗(yàn)'].value_counts().plot(kind='bar',rot=0,color='b')
plt.show()
Python熱門(mén)崗位
final = ''
stopwords = ['PYTHON','python','Python','工程師','(',')','/'] # 停止詞
for n in range(data.shape[0]):
seg_list = list(jieba.cut(data['崗位職稱(chēng)'][n]))
for seg in seg_list:
if seg not in stopwords:
final = final + seg + ' '
# final 得到的詞匯
工作地點(diǎn)
data['工作地點(diǎn)'].value_counts().plot(kind='pie',autopct='%1.2f%%',explode = np.linspace(0,1.5,25))
plt.show()
工作地理圖
# 提取數(shù)據(jù)框
data2 = list(map(lambda x:(data['工作地點(diǎn)'][x],eval(re.split('k|K',data['工資'][x])[0])*1000),range(len(data))))
# 提取價(jià)格信息
data3 = pd.DataFrame(data2)
# 轉(zhuǎn)化成Geo需要的格式
data4 = list(map(lambda x:(data3.groupby(0).mean()[1].index[x],data3.groupby(0).mean()[1].values[x]),range(len(data3.groupby(0)))))
# 地理位置展示
geo = Geo('全國(guó)Python工資布局', '制作人:挖掘機(jī)小王子', title_color='#fff', title_pos='left', width=1200, height=600,
background_color='#404a59')
attr, value = geo.cast(data4)
geo.add('', attr, value, type='heatmap', is_visualmap=True, visual_range=[0, 300], visual_text_color='#fff')
# 中國(guó)地圖Python工資,此分布是最低薪資
geo
本文作者
挖掘機(jī)小王子,數(shù)據(jù)分析愛(ài)好者。
聯(lián)系客服