00 前情提要
臨近畢業,相信大家都在找工作。而工作的薪酬,毫無疑問是我們關注的重點之一。這裏,我簡單編寫了一個Python程序,用於獲取目前市面上的工作薪酬情況。
01 實現背景
1、實習招聘網站——實習僧:https://www.shixiseng.com/,我們獲取薪酬的數據來源
2、requests模塊,用於http形式請求訪問網頁
3、BeautifulSoup模塊,用於解析獲取到的網頁內容
4、反編譯,通過逆推十六進制數字獲取對應的阿拉伯數字
02 實現目標
首先利用requests模塊獲取實習僧網頁源碼,通過BeautifulSoup模塊進一步篩選獲得薪資內容
03 注意事項
1、源代碼以python職位爲例進行薪資搜索,如需搜索其他職位,可在源代碼中自行替換對應關鍵詞
resp = requests.get('https://www.shixiseng.com/interns?page={}&keyword=python'.format(page), headers = headers)
2、網站由於反爬蟲機制,會經常替換十六進制編碼對應的數據,若薪資數字出現亂碼情況,需要自行重新逆推
3、如需將獲取內容輸入到本地文件,可自行利用with…open操作
04 實現代碼
import requests
from bs4 import BeautifulSoup
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'
}
def detial_url(url):
html = requests.get(url,headers = headers)
soup = BeautifulSoup(html.text,'lxml')
title = soup.title.text
company_name = soup.select('.com_intro .com-name ')[0].text
salary = soup.select('.job_money.cutom_font')[0].text.encode('utf-8')
salary = salary.replace(b'\xef\x8e\x8d',b'0')
salary = salary.replace(b'\xef\x8b\xbd',b'1')
salary = salary.replace(b'\xee\x8f\xbf',b'2')
salary = salary.replace(b'\xee\x96\x9a',b'3')
salary = salary.replace(b'\xef\x81\xb2',b'4')
salary = salary.replace(b'\xef\x8a\x83',b'5')
salary = salary.replace(b'\xef\x8a\xa6',b'8')
salary = salary.decode()
print(title,company_name,salary)
def crawl():
for page in range(1,5):
resp = requests.get('https://www.shixiseng.com/interns?page={}&keyword=python'.format(page), headers = headers)
html=resp.text
soup =BeautifulSoup(html,'lxml')
offers = soup.select('.intern-wrap.intern-item')
for offer in offers:
url = offer.select('.f-l.intern-detail__job a')[0]['href']
detial_url(url)
crawl()