副问题[/!--empirenews.page--]
有立场地进修
讲原理,pyspider确实是一款优越的爬虫框架,我们可以操作它快速利便地实现一个页面的抓取。
不外带来便捷性的同时,也有它的范围性,伟大页面欠好爬取。
在本次的数据爬取中,BOSS直聘是乐成行使pyspider。但拉勾网却不可,由于拉勾网的数据是Ajax加载的。
拉勾网岗亭数据哀求的网址是稳固的,改变的是表单数据,表单数据跟着页数改变,哀求方法为POST。这里没步伐在pyspider里用轮回遍素来获取每一页的数据。
大概是我对pyspider框架相识的不足,还达不到驾轻就熟。以是最后拉勾网的爬取,回收泛泛的步伐,在PyCharm中自行编写措施。
本次通过对BOSS直聘,拉勾网数据说明岗数据说明,相识数据说明岗的行业环境,也以此来相识从事数据说明所必要的手艺。
一、网页说明

获取BOSS直聘索引页信息,首要是岗亭名称、薪资、所在、事变年限、学历要求,公司名称、范例、状态、局限。
原来一开始是想对详情页说明的,还可以获取详情页里的事变内容和事变手艺需求。
然后因为哀求太多,就放弃了。索引页有10页,1页有30个岗亭,一个详情页就必要一个哀求,算起来一共有300个哀求。
我是到了第2页(60个哀求),就呈现了会见过于频仍的告诫。
而只获取索引页信息的话,只有10个哀求,根基上没什么题目,外加也不想去鼓捣署理IP,以是来点简朴的。
到时辰做数据发掘岗亭的数据时,看看放慢时刻可否获取乐成。

获取拉勾网索引页信息,首要是岗亭名称、所在、薪资、事变年限、学历要求,,公司名称、范例、状态、局限,事变手艺,事变福利。
网页为Ajax哀求,回收PyCharm编写代码,得心应手。
二、数据获取
01 pyspider获取BOSS直聘数据
pyspider的安装很简朴,直接在呼吁行pip3 install pyspider即可。
这里由于之前没有安装pyspider对接的PhantomJS(处理赏罚JavaScript渲染的页面)。
以是必要从网站下载下来它的exe文件,将其放入Python的exe文件地址的文件夹下。
最后在呼吁行输入pyspider all,即可运行pyspider。
在赏识器打开网址http://localhost:5000/,建设项目,添加项目名称,输入哀求网址,获得如下图。

最后在pyspider的剧本编辑器里编写代码,团结左边的反馈环境,对代码加以纠正。

剧本编辑器详细代码如下。
- #!/usr/bin/env python
- # -*- encoding: utf-8 -*-
- # Project: BOSS
-
- from pyspider.libs.base_handler import *
- import pymysql
- import random
- import time
- import re
-
- count = 0
-
- class Handler(BaseHandler):
- # 添加哀求头,不然呈现403报错
- 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'}}
-
- def __init__(self):
- # 毗连数据库
- self.db = pymysql.connect(host='127.0.0.1', user='root', password='774110919', port=3306, db='boss_job', charset='utf8mb4')
-
- def add_Mysql(self, id, job_title, job_salary, job_city, job_experience, job_education, company_name, company_type, company_status, company_people):
- # 将数据写入数据库中
- try:
- cursor = self.db.cursor()
- 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);
- print(sql)
- cursor.execute(sql)
- print(cursor.lastrowid)
- self.db.commit()
- except Exception as e:
- print(e)
- self.db.rollback()
-
- @every(minutes=24 * 60)
- def on_start(self):
- # 由于pyspider默认是HTTP哀求,对付HTTPS(加密)哀求,必要添加validate_cert=False,不然599/SSL报错
- 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)
-
- @config(age=10 * 24 * 60 * 60)
- def index_page(self, response):
- time.sleep(random.randint(2, 5))
- for i in response.doc('li > div').items():
- # 配置全局变量
- global count
- count += 1
- # 岗亭名称
- job_title = i('.job-title').text()
- print(job_title)
- # 岗亭薪水
- job_salary = i('.red').text()
- print(job_salary)
- # 岗亭所在
- city_result = re.search('(.*?)<em class=', i('.info-primary > p').html())
- job_city = city_result.group(1).split(' ')[0]
- print(job_city)
- # 岗亭履历
- experience_result = re.search('<em class="vline"/>(.*?)<em class="vline"/>', i('.info-primary > p').html())
- job_experience = experience_result.group(1)
- print(job_experience)
- # 岗亭学历
- job_education = i('.info-primary > p').text().replace(' ', '').replace(city_result.group(1).replace(' ', ''), '').replace(experience_result.group(1).replace(' ', ''),'')
- print(job_education)
- # 公司名称
- company_name = i('.info-company a').text()
- print(company_name)
- # 公司范例
- company_type_result = re.search('(.*?)<em class=', i('.info-company p').html())
- company_type = company_type_result.group(1)
- print(company_type)
- # 公司状态
- company_status_result = re.search('<em class="vline"/>(.*?)<em class="vline"/>', i('.info-company p').html())
- if company_status_result:
- company_status = company_status_result.group(1)
- else:
- company_status = '无信息'
- print(company_status)
- # 公司局限
- company_people = i('.info-company p').text().replace(company_type, '').replace(company_status,'')
- print(company_people + 'n')
- # 写入数据库中
- self.add_Mysql(count, job_title, job_salary, job_city, job_experience, job_education, company_name, company_type, company_status, company_people)
- # 获取下一页信息
- next = response.doc('.next').attr.href
- if next != 'javascript:;':
- self.crawl(next, callback=self.index_page, validate_cert=False)
- else:
- print("The Work is Done")
- # 详情页信息获取,因为会见次数有限定,不行使
- #for each in response.doc('.name > a').items():
- #url = each.attr.href
- #self.crawl(each.attr.href, callback=self.detail_page, validate_cert=False)
-
- @config(priority=2)
- def detail_page(self, response):
- # 详情页信息获取,因为会见次数有限定,不行使
- message_job = response.doc('div > .info-primary > p').text()
- city_result = re.findall('都市:(.*?)履历', message_job)
- experience_result = re.findall('履历:(.*?)学历', message_job)
- education_result = re.findall('学历:(.*)', message_job)
-
- message_company = response.doc('.info-company > p').text().replace(response.doc('.info-company > p > a').text(),'')
- status_result = re.findall('(.*?)d', message_company.split(' ')[0])
- people_result = message_company.split(' ')[0].replace(status_result[0], '')
-
- return {
- "job_title": response.doc('h1').text(),
- "job_salary": response.doc('.info-primary .badge').text(),
- "job_city": city_result[0],
- "job_experience": experience_result[0],
- "job_education": education_result[0],
- "job_skills": response.doc('.info-primary > .job-tags > span').text(),
- "job_detail": response.doc('div').filter('.text').eq(0).text().replace('n', ''),
- "company_name": response.doc('.info-company > .name > a').text(),
- "company_status": status_result[0],
- "company_people": people_result,
- "company_type": response.doc('.info-company > p > a').text(),
- }
(编辑:湖南网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|