您好,登錄后才能下訂單哦!
如何讓Python爬取招聘網(wǎng)站數(shù)據(jù)并做數(shù)據(jù)可視化處理,相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
基本開發(fā)環(huán)境
Python 3.6
Pycharm
相關(guān)模塊使用
爬蟲模塊
import requests import re import parsel import csv
詞云模塊
import jieba import wordcloud
目標(biāo)網(wǎng)頁分析
'https://jobs.51job.com/beijing-ftq/127676506.html?s=01&t=0'
每一個招聘信息的詳情頁都是有對應(yīng)的ID,只需要正則匹配提取ID值,通過拼接URL,然后再去招聘詳情頁提取招聘數(shù)據(jù)即可。
response = requests.get(url=url, headers=headers) lis = re.findall('"jobid":"(\d+)"', response.text) for li in lis: page_url = 'https://jobs.51job.com/beijing-hdq/{}.html?s=01&t=0'.format(li)
雖然網(wǎng)站是靜態(tài)網(wǎng)頁,但是網(wǎng)頁編碼是亂碼,在爬取的過程中需要轉(zhuǎn)碼。
f = open('招聘.csv', mode='a', encoding='utf-8', newline='') csv_writer = csv.DictWriter(f, fieldnames=['標(biāo)題', '地區(qū)', '工作經(jīng)驗(yàn)', '學(xué)歷', '薪資', '福利', '招聘人數(shù)', '發(fā)布日期']) csv_writer.writeheader() response = requests.get(url=page_url, headers=headers) response.encoding = response.apparent_encoding selector = parsel.Selector(response.text) title = selector.css('.cn h2::text').get() # 標(biāo)題 salary = selector.css('div.cn strong::text').get() # 薪資 welfare = selector.css('.jtag div.t1 span::text').getall() # 福利 welfare_info = '|'.join(welfare) data_info = selector.css('.cn p.msg.ltype::attr(title)').get().split(' | ') area = data_info[0] # 地區(qū) work_experience = data_info[1] # 工作經(jīng)驗(yàn) educational_background = data_info[2] # 學(xué)歷 number_of_people = data_info[3] # 招聘人數(shù) release_date = data_info[-1].replace('發(fā)布', '') # 發(fā)布日期 all_info_list = selector.css('div.tCompany_main > div:nth-child(1) > div p span::text').getall() all_info = '\n'.join(all_info_list) dit = { '標(biāo)題': title, '地區(qū)': area, '工作經(jīng)驗(yàn)': work_experience, '學(xué)歷': educational_background, '薪資': salary, '福利': welfare_info, '招聘人數(shù)': number_of_people, '發(fā)布日期': release_date, } csv_writer.writerow(dit) with open('招聘信息.txt', mode='a', encoding='utf-8') as f: f.write(all_info)
以上步驟即可完成關(guān)于招聘的相關(guān)數(shù)據(jù)爬取。
簡單粗略的數(shù)據(jù)清洗
薪資待遇
content = pd.read_csv(r'D:\python\demo\數(shù)據(jù)分析\招聘\招聘.csv', encoding='utf-8') salary = content['薪資'] salary_1 = salary[salary.notnull()] salary_count = pd.value_counts(salary_1)
學(xué)歷要求
content = pd.read_csv(r'D:\python\demo\數(shù)據(jù)分析\招聘\招聘.csv', encoding='utf-8') educational_background = content['學(xué)歷'] educational_background_1 = educational_background[educational_background.notnull()] educational_background_count = pd.value_counts(educational_background_1).head() print(educational_background_count) bar = Bar() bar.add_xaxis(educational_background_count.index.tolist()) bar.add_yaxis("學(xué)歷", educational_background_count.values.tolist()) bar.render('bar.html')
顯示招聘人數(shù)為無要求
工作經(jīng)驗(yàn)
content = pd.read_csv(r'D:\python\demo\數(shù)據(jù)分析\招聘\招聘.csv', encoding='utf-8') work_experience = content['工作經(jīng)驗(yàn)'] work_experience_count = pd.value_counts(work_experience) print(work_experience_count) bar = Bar() bar.add_xaxis(work_experience_count.index.tolist()) bar.add_yaxis("經(jīng)驗(yàn)要求", work_experience_count.values.tolist()) bar.render('bar.html')
詞云分析,技術(shù)點(diǎn)要求
py = imageio.imread("python.png") f = open('python招聘信息.txt', encoding='utf-8') re_txt = f.read() result = re.findall(r'[a-zA-Z]+', re_txt) txt = ' '.join(result) # jiabe 分詞 分割詞匯 txt_list = jieba.lcut(txt) string = ' '.join(txt_list) # 詞云圖設(shè)置 wc = wordcloud.WordCloud( width=1000, # 圖片的寬 height=700, # 圖片的高 background_color='white', # 圖片背景顏色 font_path='msyh.ttc', # 詞云字體 mask=py, # 所使用的詞云圖片 scale=15, stopwords={' '}, # contour_width=5, # contour_color='red' # 輪廓顏色 ) # 給詞云輸入文字 wc.generate(string) # 詞云圖保存圖片地址 wc.to_file(r'python招聘信息.png')
總結(jié):
數(shù)據(jù)分析是真的粗糙,屬實(shí)辣眼睛。
看完上述內(nèi)容,你們掌握如何讓Python爬取招聘網(wǎng)站數(shù)據(jù)并做數(shù)據(jù)可視化處理的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。