美文网首页python爬虫
Python3爬虫:(一)爬取拉勾网公司列表[已失效]

Python3爬虫:(一)爬取拉勾网公司列表[已失效]

作者: Fretice | 来源:发表于2016-04-06 22:01 被阅读2261次

<strong>已失效,大家看个思路就好</strong>

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


准备工作:

爬取前的分析:

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

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

import os
import json
import requests
import datetime
from pyquery import PyQuery as pq
from openpyxl import Workbook
from openpyxl import load_workbook


#获取城市ID列表
def get_cityId_list(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']"):
        # 修改页面中获取城市id的值的方法 :有原来的通过url获取 改为通过data_id属性获取
        aId = pq(areaId).attr("data-id")
        if(aId=='1'):
            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'} #访问参数
        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文件截图:

这一行小字应该不会有人看到的吧,Python新手 ,大牛有什么更好的建议 欢迎在评论中提出,我会努力更正,在这里先谢谢了!

相关文章

  • Python3爬虫:(一)爬取拉勾网公司列表[已失效]

    已失效,大家看个思路就好 爬取原因:Python新手,就是想了解一下Python工程师在北上广等大中城市的薪资水平...

  • 拉勾爬虫实战

    0 引言   一次简单的 Python 爬虫练习:输入 目标城市 和 目标职位,从 拉勾网 爬取相关的职位列表数据...

  • 拉勾网职位列表爬取

    三个小爬虫的最后一个是对拉勾网职位列表的爬取,当然这里没有考虑增量爬取,也没有考虑多线程爬取,仅仅是简单的把职位列...

  • Selenium小例子

    爬取腾讯动漫 爬取某网站漫画 爬取拉勾网

  • Python爬虫作业 | 爬取拉勾职位信息-Scrapy版

    由于说到Python爬虫一定绕不过Scrapy框架,所以这次也就尝试将之前的爬虫用Scrapy框架爬取拉勾网,这个...

  • 拉勾网数据两种爬取

    写在前面: 拉勾网数据爬取是一个蛮经典的爬虫案例 ,由于被频繁被爬取的原因 ,网站经过不断更新 ,加入了一些反爬技...

  • Python爬虫-拉勾网职位爬取

    感觉好久没写python了哈哈,最近都在忙工作,所以也是没有学习python。刚好凑巧朋友正在找工作,也是java...

  • 2021.04-中国裁判文书网爬虫

    2021.12.20 更新 文书网反爬已更新,此文失效。 随着反爬的不断升级,文书网的爬虫也越来越难了。 为了降低...

  • 使用requests爬取拉勾网python职位数据

    爬虫目的 本文是想通过爬取拉勾网Python相关岗位数据,简单梳理Requests和xpath的使用方法。代码部分...

  • 爬取拉勾网

    拉勾网数据加载的方式使用的是ajax异步加载的方式从后端加载数据,所以就需要分析加载的URL,如果有疑问可以看我的...

网友评论

    本文标题:Python3爬虫:(一)爬取拉勾网公司列表[已失效]

    本文链接:https://www.haomeiwen.com/subject/jhemlttx.html