岗位职责和岗位要求字段提取
在用xpath爬虫时,所有职位信息都被保存到同一个单元格,我们要根据需求,把框框中的文本提取到新的列中,同样利用正则。
主要代码内容如下:
def Classify_Keyword(path):
workbook = load_workbook(path)
sheet = workbook["Sheet2"]
n = sheet.max_row#获取行数
#根据实际情况把正则分成4种类型,首先根据有无“关键字”分成2类,然后再根据能否区分“岗位职责”和“岗位要求”分成2类
r1 = r"(.*?)((?:任职要求|任职资格|岗位要求|任职条件|任职职格|职位需求|职位要求|工作经验及所需技能).*?)职能类别:(.*?)关键字:(.*)"#?:的作用是不捕获这个括号中的内容,否则group(3)的结果是“任职要求”这个字段
r2 = r"(.*?)((?:任职要求|任职资格|岗位要求|任职条件|任职职格|职位需求|职位要求|工作经验及所需技能).*?)职能类别:(.*)"
r3 = r"(.*?)职能类别:(.*?)关键字:(.*)"
r4 = r"(.*?)职能类别:(.*)"
for i in range(2,n+1):
s = sheet.cell(i,1).value.replace("_x000D_","").replace("\n","")
sheet.cell(1,2).value = "岗位职责"
sheet.cell(1,3).value = "岗位要求"
sheet.cell(1,4).value = "职能类别"
sheet.cell(1,5).value = "关键字"
if "关键字" in s:
match = re.match(r1,s)
if match:
responbility = match.group(1)
quality = match.group(2)
classify = match.group(3)
keyword = match.group(4)
sheet.cell(i,2).value = responbility
sheet.cell(i,3).value = quality
sheet.cell(i,4).value = classify
sheet.cell(i,5).value = keyword
else:
match = re.match(r3,s)
if match:
responbility = match.group(1)
classify = match.group(2)
keyword = match.group(3)
sheet.cell(i,2).value = responbility
sheet.cell(i,4).value = classify
sheet.cell(i,5).value = keyword
else:
match = re.match(r2,s)
if match:
responbility = match.group(1)
quality = match.group(2)
classify = match.group(3)
sheet.cell(i,2).value = responbility
sheet.cell(i,3).value = quality
sheet.cell(i,4).value = classify
else:
match = re.match(r4,s)
if match:
responbility = match.group(1)
classify = match.group(2)
sheet.cell(i,2).value = responbility
sheet.cell(i,4).value = classify
workbook.save(path)