首页 > 编程语言 > 详细

Python高级应用程序设计任务

时间:2019-12-18 15:59:09      阅读:84      评论:0      收藏:0      [点我收藏+]

 

Python高级应用程序设计任务要求

用Python实现一个面向主题的网络爬虫程序,并完成以下内容:
(注:每人一题,主题内容自选,所有设计内容与源代码需提交到博客园平台)

  一、主题式网络爬虫设计方案(15分)

1.主题式网络爬虫名称
厦门人才网爬虫分析

2.主题式网络爬虫爬取的内容与数据特征分析
爬取厦门人才网公司名称、岗位名称、薪资待遇、工作地点和学历要求,并对数据进行清洗。主要对学历要求进行数据分析和数据可视化。

3.主题式网络爬虫设计方案概述(包括实现思路与技术难点)

首先,对厦门人才网的数据进行爬取,其次利用find_all()遍历方法进行爬取数据并将数据存储在recruitList.xlse表中,最后对目标数据进行数据清洗和数据可视化。

主要技术难点在于岗位关联性不高,数据太少,网页不够完整等问题,从而很难对数据进行分析可视化。

二、主题页面的结构特征分析(15分)
1.主题页面的结构特征

https://www.xmrc.com.cn/net/info/Resultg.aspx?a=a&g=g&recordtype=1&searchtype=1&keyword=Python

&releasetime=365&worklengthflag=0&sortby=updatetime&ascdesc=Desc&PageIndex=1
2.Htmls页面解析

技术分享图片

 

 

技术分享图片

 

 

技术分享图片
3.节点(标签)查找方法与遍历方法
(必要时画出节点树结构)

节点查找方法与遍历方法使用find_all()遍历页面当中属性为“class=bg” 和 “data-selected”的tr标签


三、网络爬虫程序设计(60分)
爬虫程序主体要包括以下各部分,要附源代码及较详细注释,并在每部分程序后面提供输出结果的截图。
1.数据爬取与采集

技术分享图片

技术分享图片
2.对数据进行清洗和处理

技术分享图片

技术分享图片

 

 技术分享图片 

技术分享图片

技术分享图片 

技术分享图片

技术分享图片

3.文本分析(可选):jieba分词、wordcloud可视化
4.数据分析与可视化
(例如:数据柱形图、直方图、散点图、盒图、分布图、数据回归分析等)

技术分享图片

 5.数据持久化

技术分享图片

 技术分享图片

技术分享图片

 

 

 

 技术分享图片

6.附完整程序代码

import re
import bs4
import numpy as np
import pandas as pd
import requests as req
from bs4 import BeautifulSoup


# 返回网页源码的函数
def getHTMLText(url):
    try:
        # 使用requests库爬取网页源码2
        r = req.get(
            url, headers={user-agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2226.0 Safari/537.36})
        # 判断是否爬取过程中出现错误
        r.raise_for_status()
        #  设置源码的编码格式
        r.encoding = r.apparent_encoding
        # 返回源码
        return r.text
    except:
        return "爬取失败"

# 解析页面源码并组成集合返回爬取数据
def XiamiMusicList(html):
    List = []
    # 使用bs4库中的BeautifulSoup类解析html文本
    soup = BeautifulSoup(html, "html.parser")

    trs = soup.find_all("tr","bg")
    for i in trs:
        # 招聘岗位
        a1 = i.find_all("a")[0].text.replace("\n", "")
        # 招聘链接
        a1href = i.find_all("a")[0].attrs[href].replace("\n", "")
        # 唯一id
        recruit_id = i.find_all("a")[0].attrs[href].split(
            "=")[-1].replace("\n", "")
        # 公司名称
        a2 = i.find_all("a")[1].text.replace("\n", "")
        # 公司地址
        a3 = i.find_all("a")[2].text.replace("\n", "")
        # 薪资
        a4 = i.find_all("a")[3].text.replace("\n", "")
        # 学历
        a5 = i.find_all("a")[4].text.replace("\n", "")
        # 发布日期
        a6 = i.find_all("a")[5].text.replace("\n", "")
        # 组装集合
        temp = [recruit_id, a1, a2, a3, a4, a5, a6, a1href]
        List.append(temp)
    return List
   

# 数据持久化
def weiteExcel(musicList, MusicListPath):

    # 将数据转化为DataFrame格式
    df = pd.DataFrame(musicList, columns=[
                      "recruit_id", "Occupation", "company", "locationi", "salary", "Education", "createtime", "recruit_href"])

    # 输出excel的对象
    writer = pd.ExcelWriter(MusicListPath)

    # 将数据写入到excel表格中
    df.to_excel(writer, sheet_name="recruitList")

    # 关闭输出流
    writer.save()

#  主函数
def main():
    
    Path = "https://www.xmrc.com.cn/net/info/Resultg.aspx?a=a&g=g&recordtype=1&searchtype=1&keyword="

    # 搜索关键词
    keyword = "python"

    url = "&releasetime=365&worklengthflag=0&sortby=updatetime&ascdesc=Desc&PageIndex="
    # 页数关键词// 修改此项值能获取指定页面数据
    page = "1"
    html = getHTMLText(Path+keyword+url+page)
    # print(html)

    # 存放路径
    recruitListPath = "recruitList.xlsx"

    # 获取爬取数组
    List = XiamiMusicList(html)

    # 将数据保存至文件中
    weiteExcel(List, recruitListPath)

main()
#数据清洗与可视化
import
requests as req from bs4 import BeautifulSoup import bs4 import numpy as np import pandas as pd import matplotlib.pyplot as plt import re import seaborn as sns
# 解决seaborn中文乱码问题
sns.set_style(whitegrid,{font.sans-serif:[simhei,Arial]})
# 读取文件
recruitList = pd.read_excel("recruitList.xlsx", sheet_name="recruitList")
print(recruitList.head(5))
# 数据清洗
recruitList[Occupation] = recruitList[Occupation].map(str.strip)
print(recruitList[Occupation].head(5))
print(recruitList[recruit_id].head(5))
# 数据清洗
recruitList[company] = recruitList[company].map(str.strip)
print(recruitList[company].head(5))
recruitList[locationi] = recruitList[locationi].map(str.strip)
print(recruitList[locationi].head(5))
# 数据清洗
# 对空字段填充一个默认值
recruitList[salary] = recruitList[salary].fillna(5000-7000)
recruitList[salary] = recruitList[salary].map(str.strip)
print(recruitList[salary].head(5))
# 数据清洗
# 对空字段填充一个默认值
recruitList[Education] = recruitList[Education].fillna(没要求)
recruitList[Education] = recruitList[Education].map(str.strip)
print(recruitList[Education].head(5))
#学历要求可视化
sns.boxplot(x=Education,y=recruit_id, data=recruitList)
四、结论(10分)
1.经过对主题数据的分析与可视化,可以得到哪些结论?

结论:(1)通过这次数据的分析与可视化可以很清晰的发现厦门人才网工作岗位学历要求,其中对于其他学历来说,本科学历的占比率会多一些。

 

(2)在分析的过程中由于厦门人才网里面爬取的数据比较少,关联性比较低,不容易从某个特定的职位进行数据的分析。

 

2.对本次程序设计任务完成的情况做一个简单的小结。

对于这次的网络爬虫,我选择了一个厦门人才网进行爬取,在爬取的过程中发现了很多问题,比如某个职位的招聘信息太少,无法对其进行数据可视化分析。但是通过爬取数据内容的分析,让我对厦门工作岗位也有了清晰大致的了解。最重要的是通过这次的学习让我对网络爬虫有了更深刻的理解,让我学习到之前没有接触过得知识点,提高了我的爬虫水平。

 
 

Python高级应用程序设计任务

原文:https://www.cnblogs.com/ghtt123/p/12059681.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!