人生苦短,我用Python

爬取原因:了解一下Python工程师在北上广等大中城市的薪资水平与入职前要求。


爬取前的分析:

目标网站为拉勾网 我们要获取的是网站中的所有公司的信息 通过分析翻页请求不难看出 所有数据都是通过json来传递的,所以我们只要能够正确的发送post请求,就能够获取到公司的列表数据

废话不多说,直接上代码:

[]LoadCompanyList.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import os
import json
import requests
import datetime
from pyquery import PyQuery as pq
from openpyxl import Workbook
from openpyxl import load_workbook def (url):
city_list = []
html = pq(url= url)
for areaId in html.find('#filterCollapse').find('div[class="has-more workcity"]').eq(0).find('div[class="more more-positions"]').find("a[data-lg-tj-cid='idnull']"):
aId = pq(areaId).attr('href').replace('http://www.lagou.com/gongsi/', '').replace('-0-0#filterBox', '')
if(aId=='0'):
continue
city_list.append(aId)
return city_list #获取城市名称列表
def get_city_name_list(u):
city_name_list = []
url = 'http://www.lagou.com/gongsi/'
html = pq(url=url)
for areaId in html.find('#filterCollapse').find('div[class="has-more workcity"]').eq(0).find('div[class="more more-positions"]').find("a[data-lg-tj-cid='idnull']"):
area_name=pq(areaId).html()
if area_name=="全国":
continue
city_name_list.append(area_name)
return city_name_list #获取城市下一共有多少页
def get_city_page(areaId,page_num):
try:
param = {'first': 'false', 'pn': page_num, 'sortField': '0', 'havemark': '0'} 大专栏  Python3爬虫:(一)爬取拉勾网公司列表#访问参数
r = requests.post('http://www.lagou.com/gongsi/'+areaId+'-0-0.json',params=param ) #requsets请求
page_num += 1
if(len(r.json()['result'])/16==1):
return get_city_page(areaId,page_num)
else:
return page_num
except:
return page_num-1 #根据城市ID获取所有公司信息
def get_company_list(areaId):
company_list = []
city_page_total=get_city_page(areaId,1)
for pageIndex in range(1,city_page_total):
print('正在爬取第'+str(pageIndex)+'页')
json_url = 'http://www.lagou.com/gongsi/'+areaId+'-0-0.json'
param = {'first': 'false', 'pn': str(pageIndex), 'sortField': '0', 'havemark': '0'} #访问参数
r = requests.post(json_url,params=param ) #requsets请求
msg = json.loads(r.text)
try:
for company in msg['result']:
company_list.append([company['city'],company['cityScore'],company['companyFeatures'],company['companyId'],company['companyLabels'],company['companyLogo'],company['companyName'],str(company['companyPositions']),company['companyShortName'],company['countryScore'],company['createTime'],company['finaceStage'],company['industryField'],company['interviewRemarkNum'],company['otherLabels'], company['positionNum'],company['processRate'],str(datetime.datetime.now())])
except:
print('爬取编号为'+str(areaId)+'城市时第'+str(pageIndex)+'页出现了错误,错误时请求返回内容为:'+str(msg))
continue
return company_list #写入Excel文件方法
def write_file(fileName):
list = []
wb = Workbook()
ws = wb.active
url = 'http://www.lagou.com/gongsi/'
area_name_list = get_city_name_list(url)
for area_name in area_name_list:
wb.create_sheet(title = area_name)
file_name = fileName+'.xlsx'
wb.save(file_name)
areaId_list = get_cityId_list(url)
for areaId in areaId_list:
company_list = get_company_list(areaId)
print('正在爬取----->****'+company_list[0][0]+'****公司列表')
wb1 = load_workbook(file_name)
ws = wb1.get_sheet_by_name(company_list[0][0])
ws.append(['城市名称','城市得分','公司期望','公司ID','公司标签','公司Logo','发展阶段','企业名称','企业位置','企业简称','注册时间','财务状况','行业','在招职位','其他标签','简历处理率'])
for company in company_list:
ws.append([company[0],str(company[1]),company[2],str(company[3]),company[4],company[5],company[6],company[7],company[8],company[9],company[10],company[11],company[12],company[13],company[14],company[15]])
wb1.save(file_name) file_name = input('请输入文件名称')
print(str(datetime.datetime.now()))
write_file(file_name)
print(str(datetime.datetime.now()))

废话两句:

此类招聘网站的目标人群是所有人,不会被限制爬虫,可以放心的爬。

本人爬取出所有的公司数据用了 45分钟, 数据比较少就没考虑用多进程爬虫 ,存储到excel中的公司名称一共有27k家的公司左右,与官网页面宣传的差了很多,不知道是不是因为很多企业没有认证的原因。
最后奉上爬取的Excel文件截图:


Python3爬虫:(一)爬取拉勾网公司列表的更多相关文章

  1. python-scrapy爬虫框架爬取拉勾网招聘信息

    本文实例为爬取拉勾网上的python相关的职位信息, 这些信息在职位详情页上, 如职位名, 薪资, 公司名等等. 分析思路 分析查询结果页 在拉勾网搜索框中搜索'python'关键字, 在浏览器地址栏 ...

  2. 【Python3爬虫】爬取美女图新姿势--Redis分布式爬虫初体验

    一.写在前面 之前写的爬虫都是单机爬虫,还没有尝试过分布式爬虫,这次就是一个分布式爬虫的初体验.所谓分布式爬虫,就是要用多台电脑同时爬取数据,相比于单机爬虫,分布式爬虫的爬取速度更快,也能更好地应对I ...

  3. 通俗易懂的分析如何用Python实现一只小爬虫,爬取拉勾网的职位信息

    源代码:https://github.com/nnngu/LagouSpider 效果预览 思路 1.首先我们打开拉勾网,并搜索"java",显示出来的职位信息就是我们的目标. 2 ...

  4. python3 爬虫之爬取安居客二手房资讯(第一版)

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- # Author;Tsukasa import requests from bs4 import Beau ...

  5. Python3爬虫之爬取某一路径的所有html文件

    要离线下载易百教程网站中的所有关于Python的教程,需要将Python教程的首页作为种子url:http://www.yiibai.com/python/,然后按照广度优先(广度优先,使用队列:深度 ...

  6. python3爬虫应用--爬取网易云音乐(两种办法)

    一.需求 好久没有碰爬虫了,竟不知道从何入手.偶然看到一篇知乎的评论(https://www.zhihu.com/question/20799742/answer/99491808),一时兴起就也照葫 ...

  7. 【图文详解】scrapy爬虫与动态页面——爬取拉勾网职位信息(2)

    上次挖了一个坑,今天终于填上了,还记得之前我们做的拉勾爬虫吗?那时我们实现了一页的爬取,今天让我们再接再厉,实现多页爬取,顺便实现职位和公司的关键词搜索功能. 之前的内容就不再介绍了,不熟悉的请一定要 ...

  8. node.js爬虫爬取拉勾网职位信息

    简介 用node.js写了一个简单的小爬虫,用来爬取拉勾网上的招聘信息,共爬取了北京.上海.广州.深圳.杭州.西安.成都7个城市的数据,分别以前端.PHP.java.c++.python.Androi ...

  9. 一起学爬虫——使用selenium和pyquery爬取京东商品列表

    layout: article title: 一起学爬虫--使用selenium和pyquery爬取京东商品列表 mathjax: true --- 今天一起学起使用selenium和pyquery爬 ...

随机推荐

  1. 1.windows-oracle实战第一课

    一.oracle是目前最流行的数据库之一,功能强大.性能卓越.学习要有信心.oracle也做软件,不仅仅是数据库.比如ERP(企业资源计划,用友.金蝶) 二.目前的数据库   相对而言:       ...

  2. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第四天】

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

  3. c++ string 的注意事项

    string 的reserve 和 resize 区别 reserve 与capacity相关,如果reserve一个比当前capacity大的 reserve 只会找到一块合适大小的内存,并将原始内 ...

  4. Complier

    Complier [2019福建省赛] 模拟题应该有信心写,多出一些样例 当/* 与// 在一起的时候总会出错,一旦出现了这些有效的 应该把它删掉不对后面产生影响 #include<bits/s ...

  5. NAIPC2018

    NAIPC2018 参考:http://www.cnblogs.com/LQLlulu/p/9513669.html?tdsourcetag=s_pctim_aiomsg https://www.cn ...

  6. 吴裕雄--天生自然C语言开发:判断

    if(boolean_expression) { /* 如果布尔表达式为真将执行的语句 */ } #include <stdio.h> int main () { /* 局部变量定义 */ ...

  7. jsp 页面内容导出到Excel中

    日常使用网络资源时经常需要把网页中的内容下载到本地,并且导出到Excel中,现在介绍一种非常简单的方式实现网络资源的下载.只需要讲jsp的最上面加上一句话 <% response.reset() ...

  8. VirtualBox虚拟机Ubuntu设置共享文件夹,并自动挂载

    一.环境 Win10系统,VirtualBox-5.1.22-115126+Ubuntu16.04(64位)虚拟机   二.目的 在Ubuntu中能够共享Win10中的某个文件夹,而且能够自动挂载   ...

  9. iOS动画效果集合、 通过摄像头获取心率、仿淘宝滑动样式、瀑布流、分类切换布局等源码

    iOS精选源码 动画知识运用及常见动画效果收集 较为美观的多级展开列表 MUImageCache -简单轻量的图片缓存方案 iOS 瀑布流之栅格布局 一用就上瘾的JXCategoryView iOS ...

  10. plsql登录,tables表为空解决方案

    共两种方法,第一种不行,再试下第二种: 第一种: plsql tables 表存在,但是看不到所有的表信息 将C:\Windows\Prefetch目录下,几个PLSQL DEVELOPER***** ...