python爬虫实战之爬取51job前程无忧简历

首先F12对搜索的网页进行分析, 我们可以观察到,其网页结构比较简单,基本信息都在 p标签下 这种情况利用正则表达式可以很容易的把信息提取出来

代码如下:

import urllib.request
import re

#获取原码
def get_content(page,name):
    name = urllib.request.quote(name)
    url =http://search.51job.com/list/000000,000000,0000,00,9,99,+name+,2,+ str(page)+.html
    a = urllib.request.urlopen(url)#打开网址
    html = a.read().decode(gbk)#读取源代码并转为unicode
    return html

def get(html):
    reg1 = re.compile(rclass="t1 ">.*?<a target="_blank" title=".*?" href="(.*?)".*?<span class="t2">, re.S)#公司招人详情
    detail_url=re.findall(reg1, html)
    print(detail_url)
    reg = re.compile(rclass="t1 ">.*? <a target="_blank" title="(.*?)".*?<span class="t2"><a target="_blank" title="(.*?)" href="(.*?)".*?<span class="t3">(.*?)</span>.*?<span class="t4">(.*?)</span>.*? <span class="t5">(.*?)</span>,re.S)#基本信息
    items=re.findall(reg,html)
    return items,detail_url
def run():
    name = input(请输入想要爬取的职业:)
    #多页处理,下载到文件
    for  j in range(1,3):
        print("正在爬取第"+str(j)+"页数据...")
        html=get_content(j,name)#调用获取网页原码
        items, detail_url=get(html)
        for i,c in zip(items,detail_url):
            #print(i[0],i[1],i[2],i[3],i[4])
            with open (51job.txt,a,encoding=utf-8) as f:
                f.write(i[0]+	+i[1]+	+i[3]+	+i[4]+	+i[5]+	+i[2]+	+c+
)



                f.close()
if __name__ == __main__:
    run()

演示如下: txt文件:

总结:本代码只是对搜索网页上的职位进行简单的爬取,后续将将对detail_url网页内的职业内容详情进行爬取,并进行数据清洗等操作,对数据文本进行挖掘与分析。

**对于51job详情爬取并生成Excel文件请移步这篇文章:**

经验分享 程序员 微信小程序 职场和发展