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

Python数据可视化:浅谈数据说明岗

发布时间:2018-12-05 20:25:03 所属栏目:教程 来源:法纳斯特
导读:有立场地进修 讲原理,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.         } 

(编辑:湖南网)

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

热点阅读