zoukankan      html  css  js  c++  java
  • Python爬虫学习之使用beautifulsoup爬取招聘网站信息

    菜鸟一只,也是在尝试并学习和摸索爬虫相关知识。

    1.首先分析要爬取页面结构。可以看到一列搜索的结果,现在需要得到每一个链接,然后才能爬取对应页面。

    关键代码思路如下:

    html = getHtml("http://www.zhrczp.com/jobs/jobs_list/key/%E5%BB%BA%E6%98%8E%E9%95%87/page/1.html")
    
    soup = BeautifulSoup(html, 'lxml')  #声明BeautifulSoup对象
    
    hrefbox = soup.find_all("div","td-j-name",True);
    
    links = [];
    for href in range(0,len(hrefbox)):    
        links.append("http://www.zhrczp.com"+hrefbox[href].contents[0].get('href'));#拼接链接

    现在已经得到一系列链接,下面分析需要爬取的链接页面的结构

     2.分析页面,页面所有感兴趣的内容均在 div标签里面,可以使用beautifulsoup提供的find_all函数来查找。

    main = soup.find_all("div","main",True); 意思是查找div标签class为main的内容

    爬取并保存文件,效果如下:

    详细代码如下:

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    import urllib
    from bs4 import BeautifulSoup
    
    def getHtml(url):
        page = urllib.request.urlopen(url)
        html = page.read()
        return html
    
    html = getHtml("http://www.zhrczp.com/jobs/jobs_list/key/%E5%BB%BA%E6%98%8E%E9%95%87/page/1.html")
    
    soup = BeautifulSoup(html, 'lxml')  #声明BeautifulSoup对象
    
    hrefbox = soup.find_all("div","td-j-name",True);
    
    links = [];
    for href in range(0,len(hrefbox)):    
        links.append("http://www.zhrczp.com"+hrefbox[href].contents[0].get('href'));#拼接链接
    
    f=open('a.txt','w',encoding='utf-8')
    for link in links:
        print(link);
        html = getHtml(link)
        soup = BeautifulSoup(html, 'lxml')  #声明BeautifulSoup对象
        
        main = soup.find_all("div","main",True);
        f.write("
    **********************************************************************
    
    ")
        f.write("职位名称:"+main[0].contents[1].contents[5].contents[1].contents[0]+"
    ");#职位名称
        f.write("发布时间:"+main[0].contents[1].contents[3].contents[1].contents[0]+"
    ");#发布时间
        f.write("
    --------------------职位待遇--------------------
    ");
        f.write("工资:"+main[0].contents[1].contents[7].contents[0]+"
    ");#wage
        f.write("福利:");
        for i in range(1,len(main[0].contents[1].contents[9].contents)-3):
            f.write(main[0].contents[1].contents[9].contents[i].contents[0]+" ");
            
        f.write("
    
    --------------------联系方式--------------------
    ")
        f.write(main[0].contents[5].contents[3].contents[0].strip()+"
    ");#联系人 去掉空格
        f.write(main[0].contents[5].contents[7].contents[0]+main[0].contents[5].contents[7].contents[1].contents[0]+"
    ");#联系电话
        
        f.write("
    --------------------联系描述--------------------
    ")
        describe = main[0].contents[7].contents;
        f.write(describe[1].contents[0]+describe[3].contents[0]+"
    ");#职位描述
        
        item = soup.find_all("div","item",True);
        f.write("
    --------------------职位要求--------------------
    ");
        f.write(item[0].contents[3].contents[0].contents[0]+":"+item[0].contents[3].contents[1]+"
    ");#工作性质
        f.write(item[0].contents[5].contents[0].contents[0]+":"+item[0].contents[5].contents[1]+"
    ");#职位类别
        f.write(item[0].contents[7].contents[0].contents[0]+":"+item[0].contents[7].contents[1]+"
    ");#招聘人数
        f.write(item[0].contents[11].contents[0].contents[0]+":"+item[0].contents[11].contents[1]+"
    ");#学历要求
        f.write(item[0].contents[13].contents[0].contents[0]+":"+item[0].contents[13].contents[1]+"
    ");#工作经验
        f.write(item[0].contents[15].contents[0].contents[0]+":"+item[0].contents[15].contents[1]+"
    ");#性别要求
        f.write(item[0].contents[19].contents[0].contents[0]+":"+item[0].contents[19].contents[1]+"
    ");#年龄要求
        f.write(item[0].contents[21].contents[0].contents[0]+":"+item[0].contents[21].contents[1]+"
    ");#招聘部门
        f.write(item[0].contents[25].contents[0].contents[0]+":"+item[0].contents[25].contents[1]+"
    ");#招聘部门
        
        company = soup.find_all("div","cominfo link_gray6",True);
        f.write("
    --------------------公司信息--------------------
    ");
        f.write(company[0].contents[3].contents[1].contents[0]+"
    ");#公司名称
        f.write(company[0].contents[5].contents[0].contents[0]+":"+company[0].contents[5].contents[1]+"
    ");#公司性质
        f.write(company[0].contents[7].contents[0].contents[0]+":"+company[0].contents[7].contents[1]+"
    ");#公司行业
        f.write(company[0].contents[9].contents[0].contents[0]+":"+company[0].contents[9].contents[1]+"
    ");#公司规模
        f.write(company[0].contents[11].contents[0].contents[0]+":"+company[0].contents[11].contents[1]+"
    ");#公司地区
        
        f.write("
    **********************************************************************
    ")
    f.close();

     参考:

    http://www.cnblogs.com/Albert-Lee/p/6232745.html

    https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html

  • 相关阅读:
    贴现值计算
    基于“淘宝”的六种质量属性场景描绘
    Maven 学习笔记1
    酷客机器学习十讲笔记10
    酷客机器学习十讲笔记9
    酷客机器学习十讲8
    酷客机器学习十讲笔记7
    酷客机器学习十讲笔记6
    酷客机器学习十讲笔记5
    前后端分离项目学习笔记
  • 原文地址:https://www.cnblogs.com/hellowooorld/p/8075605.html
Copyright © 2011-2022 走看看