00 目標提出
設計一個支持多個招聘網站的檢索,可以通過指定目標城市、檢索職業和查詢數量,個性化輸出檢索結果,爾後將結果顯示在界面並保存於文檔中。
01 基本思路
02 核心代碼
class JobCrawler:
def __init__(self, city, jobName, page):
self.city = city
self.jobName = jobName
self.page = page
def get_jobs_51job(self):
citys = {'北京': '010000', }
try :
cityID = citys[self.city]
except:
cityID = "000000"
url = "https://search.51job.com/list/{},000000,0000,00,9,99,{},2,{}.html".format(cityID, self.jobName, str(self.page))
response = requests.get(url, headers = HEADERS)
html = etree.HTML(response.text)
contents = html.xpath("//div[@class='dw_table']/div[@class='el']")
jobs = []
for content in contents:
try:
jobs.append(job)
except:
pass
return jobs
def get_jobs_zhilian(self):
url = "https://fe-api.zhaopin.com/c/i/sou?"
params = {"start":(self.page-1)*50, "cityId":self.city, "kt":3, "kw":self.jobName, "pageSize":50}
response = requests.get(url = url, headers = HEADERS, params = params)
dic_results = json.loads(response.text)
results = dic_results['data']['results']
jobs = []
for result in results:
try:
jobs.append(job)
except:
pass
return jobs
def get_jobs_liepin(self):
citys = {'北京': 'city-bj/', }
try :
cityID = citys[self.city]
except:
cityID = ""
url = "https://www.liepin.com/{0}zhaopin/pn{2}/?key={1}&pageSize=50".format(cityID, self.jobName, str(self.page-1))
response = requests.get(url, headers = HEADERS)
html = etree.HTML(response.text)
joblist = html.xpath('//ul[@data-selector="result-list"]/li')
jobs=[]
for job in joblist:
try:
jobs.append(jobinfo)
except:
pass
return jobs
class JobHelper(object):
def __init__(self):
qfile_jh = QFile("ui/jobhelper.ui")
qfile_jh.open(QFile.ReadOnly)
qfile_jh.close()
self.ui = QUiLoader().load(qfile_jh)
self.ui.plainTextEdit.setReadOnly(True)
self.ui.pushButton_search.clicked.connect(self.search)
self.ui.pushButton_clear.clicked.connect(self.clearJobs)
def search(self):
Web = self.ui.comboBox_PT.currentText()
if self.ui.radioButton_select.isChecked():
city = self.ui.comboBox_citys.currentText()
elif self.ui.radioButton_enter.isChecked():
city = self.ui.lineEdit_cityInput.text()
if not city.strip():
QMessageBox.about(self.ui,'Warning',"請輸入城市")
return
else :
try:
with open("ui/location.txt","r") as f:
citys = f.read()
citys = citys.split("\n")
if city not in citys:
QMessageBox.about(self.ui,'Warning',"輸入城市可能有誤…")
return
except:
QMessageBox.about(self.ui,'Warning',"讀取城市列表失敗")
return
else:
QMessageBox.about(self.ui,'Warning',"請選擇城市")
return
jobName = self.ui.lineEdit_jobName.text()
if not jobName.strip():
QMessageBox.about(self.ui,'Warning',"請輸入待查詢職業")
return
return
def clearJobs(self):
self.ui.plainTextEdit.setPlainText("")
return
def mylen(s):
mylen = len(s)
for c in s:
if u'\u4e00' <= c <= u'\u9fa5':
mylen += 1
return mylen
def setstr(s, length):
if mylen(s) <= length:
return s + ' ' * (length - mylen(s))
else:
for index in range(int(length / 2) - 4, length - 3):
ss = s[0:index]
if mylen(ss) >= length - 3:
break
return ss + "." * (length - mylen(ss))
03 運行情況
啟動界面
檢索結果示例
點個在看可好