加入收藏 | 设为首页 | 会员中心 | 我要投稿 核心网 (https://www.hxwgxz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

Python数据可视化:浅谈数据分析岗

发布时间:2018-12-04 06:22:54 所属栏目:教程 来源:法纳斯特
导读:有态度地学习 讲道理,pyspider确实是一款优秀的爬虫框架,我们可以利用它快速方便地实现一个页面的抓
副标题[/!--empirenews.page--]

有态度地学习

讲道理,pyspider确实是一款优秀的爬虫框架,我们可以利用它快速方便地实现一个页面的抓取。

不过带来便捷性的同时,也有它的局限性,复杂页面不好爬取。

在本次的数据爬取中,BOSS直聘是成功使用pyspider。但拉勾网却不行,因为拉勾网的数据是Ajax加载的。

拉勾网岗位数据请求的网址是不变的,改变的是表单数据,表单数据随着页数改变,请求方式为POST。这里没办法在pyspider里用循环遍历来获取每一页的数据。

也许是我对pyspider框架了解的不够,还达不到得心应手。所以最后拉勾网的爬取,采用平常的办法,在PyCharm中自行编写程序。

本次通过对BOSS直聘,拉勾网数据分析岗数据分析,了解数据分析岗的行业情况,也以此来了解从事数据分析所需要的技能。

一、网页分析

Python数据可视化:浅谈数据分析岗

获取BOSS直聘索引页信息,主要是岗位名称、薪资、地点、工作年限、学历要求,公司名称、类型、状态、规模。

本来一开始是想对详情页分析的,还可以获取详情页里的工作内容和工作技能需求。

然后由于请求太多,就放弃了。索引页有10页,1页有30个岗位,一个详情页就需要一个请求,算起来一共有300个请求。

我是到了第2页(60个请求),就出现了访问过于频繁的警告。

而只获取索引页信息的话,只有10个请求,基本上没什么问题,外加也不想去鼓捣代理IP,所以来点简单的。

到时候做数据挖掘岗位的数据时,看看放慢时间能否获取成功。

Python数据可视化:浅谈数据分析岗

获取拉勾网索引页信息,主要是岗位名称、地点、薪资、工作年限、学历要求,,公司名称、类型、状态、规模,工作技能,工作福利。

网页为Ajax请求,采用PyCharm编写代码,轻车熟路。

二、数据获取

01 pyspider获取BOSS直聘数据

pyspider的安装很简单,直接在命令行pip3 install pyspider即可。

这里因为之前没有安装pyspider对接的PhantomJS(处理JavaScript渲染的页面)。

所以需要从网站下载下来它的exe文件,将其放入Python的exe文件所在的文件夹下。

最后在命令行输入pyspider all,即可运行pyspider。

在浏览器打开网址http://localhost:5000/,创建项目,添加项目名称,输入请求网址,得到如下图。

Python数据可视化:浅谈数据分析岗

最后在pyspider的脚本编辑器里编写代码,结合左边的反馈情况,对代码加以改正。

Python数据可视化:浅谈数据分析岗

脚本编辑器具体代码如下。

  1. #!/usr/bin/env python 
  2. # -*- encoding: utf-8 -*- 
  3. # Project: BOSS 
  4.  
  5. from pyspider.libs.base_handler import * 
  6. import pymysql 
  7. import random 
  8. import time 
  9. import re 
  10.  
  11. count = 0 
  12.  
  13. class Handler(BaseHandler): 
  14.     # 添加请求头,否则出现403报错 
  15.     crawl_config = {'headers': {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}} 
  16.  
  17.     def __init__(self): 
  18.         # 连接数据库 
  19.         self.db = pymysql.connect(host='127.0.0.1', user='root', password='774110919', port=3306, db='boss_job', charset='utf8mb4') 
  20.  
  21.     def add_Mysql(self, id, job_title, job_salary, job_city, job_experience, job_education, company_name, company_type, company_status, company_people): 
  22.         # 将数据写入数据库中 
  23.         try: 
  24.             cursor = self.db.cursor() 
  25.             sql = 'insert into job(id, job_title, job_salary, job_city, job_experience, job_education, company_name, company_type, company_status, company_people) values ("%d", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s")' % (id, job_title, job_salary, job_city, job_experience, job_education, company_name, company_type, company_status, company_people); 
  26.             print(sql) 
  27.             cursor.execute(sql) 
  28.             print(cursor.lastrowid) 
  29.             self.db.commit() 
  30.         except Exception as e: 
  31.             print(e) 
  32.             self.db.rollback() 
  33.  
  34.     @every(minutes=24 * 60) 
  35.     def on_start(self): 
  36.         # 因为pyspider默认是HTTP请求,对于HTTPS(加密)请求,需要添加validate_cert=False,否则599/SSL报错 
  37.         self.crawl('https://www.zhipin.com/job_detail/?query=%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90&scity=100010000&industry=&position=', callback=self.index_page, validate_cert=False) 
  38.  
  39.     @config(age=10 * 24 * 60 * 60) 
  40.     def index_page(self, response): 
  41.         time.sleep(random.randint(2, 5)) 
  42.         for i in response.doc('li > div').items(): 
  43.             # 设置全局变量 
  44.             global count 
  45.             count += 1 
  46.             # 岗位名称 
  47.             job_title = i('.job-title').text() 
  48.             print(job_title) 
  49.             # 岗位薪水 
  50.             job_salary = i('.red').text() 
  51.             print(job_salary) 
  52.             # 岗位地点 
  53.             city_result = re.search('(.*?)<em class=', i('.info-primary > p').html()) 
  54.             job_city = city_result.group(1).split(' ')[0] 
  55.             print(job_city) 
  56.             # 岗位经验 
  57.             experience_result = re.search('<em class="vline"/>(.*?)<em class="vline"/>', i('.info-primary > p').html()) 
  58.             job_experience = experience_result.group(1) 
  59.             print(job_experience) 
  60.             # 岗位学历 
  61.             job_education = i('.info-primary > p').text().replace(' ', '').replace(city_result.group(1).replace(' ', ''), '').replace(experience_result.group(1).replace(' ', ''),'') 
  62.             print(job_education) 
  63.             # 公司名称 
  64.             company_name = i('.info-company a').text() 
  65.             print(company_name) 
  66.             # 公司类型 
  67.             company_type_result = re.search('(.*?)<em class=', i('.info-company p').html()) 
  68.             company_type = company_type_result.group(1) 
  69.             print(company_type) 
  70.             # 公司状态 
  71.             company_status_result = re.search('<em class="vline"/>(.*?)<em class="vline"/>', i('.info-company p').html()) 
  72.             if company_status_result: 
  73.                 company_status = company_status_result.group(1) 
  74.             else: 
  75.                 company_status = '无信息' 
  76.             print(company_status) 
  77.             # 公司规模 
  78.             company_people = i('.info-company p').text().replace(company_type, '').replace(company_status,'') 
  79.             print(company_people + 'n') 
  80.             # 写入数据库中 
  81.             self.add_Mysql(count, job_title, job_salary, job_city, job_experience, job_education, company_name, company_type, company_status, company_people) 
  82.         # 获取下一页信息 
  83.         next = response.doc('.next').attr.href 
  84.         if next != 'javascript:;': 
  85.             self.crawl(next, callback=self.index_page, validate_cert=False) 
  86.         else: 
  87.             print("The Work is Done") 
  88.         # 详情页信息获取,由于访问次数有限制,不使用 
  89.         #for each in response.doc('.name > a').items(): 
  90.             #url = each.attr.href 
  91.             #self.crawl(each.attr.href, callback=self.detail_page, validate_cert=False) 
  92.  
  93.     @config(priority=2) 
  94.     def detail_page(self, response): 
  95.         # 详情页信息获取,由于访问次数有限制,不使用 
  96.         message_job = response.doc('div > .info-primary > p').text() 
  97.         city_result = re.findall('城市:(.*?)经验', message_job) 
  98.         experience_result = re.findall('经验:(.*?)学历', message_job) 
  99.         education_result = re.findall('学历:(.*)', message_job) 
  100.  
  101.         message_company = response.doc('.info-company > p').text().replace(response.doc('.info-company > p > a').text(),'') 
  102.         status_result = re.findall('(.*?)d', message_company.split(' ')[0]) 
  103.         people_result = message_company.split(' ')[0].replace(status_result[0], '') 
  104.  
  105.         return { 
  106.             "job_title": response.doc('h1').text(), 
  107.             "job_salary": response.doc('.info-primary .badge').text(), 
  108.             "job_city": city_result[0], 
  109.             "job_experience": experience_result[0], 
  110.             "job_education": education_result[0], 
  111.             "job_skills": response.doc('.info-primary > .job-tags > span').text(), 
  112.             "job_detail": response.doc('div').filter('.text').eq(0).text().replace('n', ''), 
  113.             "company_name": response.doc('.info-company > .name > a').text(), 
  114.             "company_status": status_result[0], 
  115.             "company_people": people_result, 
  116.             "company_type": response.doc('.info-company > p > a').text(), 
  117.         } 

(编辑:核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读