python3 + re + requests + mysql.connector爬取赶集网招聘信息存储到MySQL 中

使用正则表达式来爬取赶集网的招聘信息存储到MySQL

输入想要爬取的工作岗位的名称

job_name=input("请输入你想要找的职位:")
url='http://cd.ganji.com/zhaopin/s/'+'_'+job_name

首先爬取该工作所有的招聘信息的岗位名称及该工作的详细描述url(用正则提取,由于工作名称中有<span>标签,需把它删除),并把工作名称和url存储到mysql的表(types_info)中:

def get_type_info(html):
    pattern=re.compile('<div class="fl j-title">.*?'
                +'<a href="(.*?)".*?>(.*?)</a>',re.S)
    items=re.findall(pattern,html)
    data=[]
    for item in items:
        item=list(item)
        item[0]=BASE_URL+item[0]
        item[1]=item[1].strip().replace("<span class='f_c_red'>","")
        item[1]=item[1].replace("</span>","")
        item=tuple(item)
        data.append(item)
    return data

从数据表(types_info)查询到所有工作岗位的url,然后爬取更加详细的工作信息,包含:名称,薪资,公司地点,公司名称,岗位的具体能力要求,并将信息存入数据表(jobs_info)中:

def get_jobs_info(url):
    html=get_html(url)
    #工作名称
    pattern1=re.compile('<div class="title-line clearfix">.*?<p>(.*?)</p>',re.S)
    job=re.findall(pattern1,html)
    #薪资
    pattern2=re.compile('<div class="salary-line">.*?<b>(.*?)</b>',re.S)
    salary=re.findall(pattern2,html)
    #工作地点
    pattern3=re.compile('id="viewMap">.*?<p class="detail-map-top">'
                        +'<i class="ico-coordinate"></i>(.*?)</p>',re.S)
    location=re.findall(pattern3,html)
    location=''.join(location)#list列表转为字符串
    location=location.strip().replace(" ","")
    #公司名称
    pattern4=re.compile('@hclick=companyname" >(.*?)</a>',re.S)
    company=re.findall(pattern4,html)
    #任职要求
    pattern5=re.compile('data-role="description">(.*?)</div>',re.S)
    demand=re.findall(pattern5,html)
    demand=''.join(demand).strip().replace("<br>","")
    demand=demand.replace("<br />","")
    demand=demand.replace("\r","")
    #转化为一个list列表
    location=list(location.split())
    items=job+salary+location+company
    items.append(demand)
    print(items)
    return items

关于MySQL的代码:

db=mysql.connector.connect(
    host="localhost",
    user="root",
    passwd="root",
    database="exercise"
    )
print(db)
mycursor=db.cursor()

#创建表1types_info
mycursor.execute("CREATE TABLE IF NOT EXISTS types_info(url VARCHAR(500),\
                    name VARCHAR(500))ENGINE=InnoDB DEFAULT CHARSET=utf8")
#创建表2jobs_info
mycursor.execute("CREATE TABLE IF NOT EXISTS jobs_info(job_name VARCHAR(500),\
                 salary VARCHAR(500),location VARCHAR(10000),company VARCHAR(500),\
                 demand VARCHAR(5000))ENGINE=InnoDB DEFAULT CHARSET=utf8")

def insert_info(table,item):
    #插入数据
    values=','.join(['%s']*len(item))
    sql="INSERT INTO %s  VALUES (%s)" %(table,values)
    mycursor.execute(sql,tuple(item))
    db.commit()
    print("记录插入成功")
def select_info(table,value):
    sql="SELECT %s FROM %s" %(value,table)
    mycursor.execute(sql)
    return mycursor.fetchall()
def close_conn():
    mycursor.close()
    db.close()

输入java工程师,爬取所得如下所示:

表types_info:

表jobs_info:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值