1. 程式人生 > >爬取拉勾

爬取拉勾

用法:

1-配置環境2.7版本,安裝好模組

2-cookies值修改 在chrome按F12

     

3-修改地區和崗位

4-經過上述修改即可爬取了

程式碼如下(新增cookies值,修改地區  ,崗位,可以直接爬取

#!/usr/bin/python
#-*- coding:utf-8 -*-
#created 2018.11.11

import json
import requests
import xlwt
import time
from lxml import etree

#解決編碼的問題
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

#獲取儲存職位資訊的json物件,遍歷獲得公司名、福利待遇、工作地點、學歷要求、工作型別、釋出時間、職位名稱、薪資、工作年限
def get_json(url,datas):


    my_headers = {
        'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Host': 'www.lagou.com',
        'Origin': 'https://www.lagou.com',
        'Referer': 'https://www.lagou.com/jobs/list_python?city=%E4%B8%8A%E6%B5%B7&cl=false&fromSearch=true&labelWords=&suginput=',
    }
    cookies = {
        ##修改cookies
    }
    time.sleep(8)
    content = requests.post(url=url,cookies=cookies,headers=my_headers,data=datas)
    # content.encoding = 'utf-8'
    result = content.json()
    print result
    info = result['content']['positionResult']['result']
    # print info
    info_list = []
    for job in info:
        information = []
        information.append(job['positionId']) #崗位對應ID
        information.append(job['companyFullName']) #公司全名
        information.append(job['companyLabelList']) #福利待遇
        information.append(job['district']) #工作地點
        information.append(job['education']) #學歷要求
        information.append(job['firstType']) #工作型別
        information.append(job['formatCreateTime']) #釋出時間
        information.append(job['positionName']) #職位名稱
        information.append(job['salary']) #薪資
        information.append(job['workYear']) #工作年限
        info_list.append(information)
        #將列表物件進行json格式的編碼轉換,其中indent引數設定縮排值為2
        print json.dumps(info_list,ensure_ascii=False,indent=2)
        print info_list
    return info_list


def main():
    page = int(raw_input('請輸入你要抓取的頁碼總數:'))
#    kd = raw_input('請輸入你要抓取的職位關鍵字:')
 #   city = raw_input('請輸入你要抓取的城市:')


    info_result = []
    title = ['崗位id','公司全名','福利待遇','工作地點','學歷要求','工作型別','釋出時間','職位名稱','薪資','工作年限']
    info_result.append(title)
    for x in range(1,page+1):
        url = 'https://www.lagou.com/jobs/positionAjax.json?&needAddtionalResult=false'
        datas = {
            'first': True,
            'pn': x,
            'kd': '運維工程師',
            'city': '北京'
        }
        info = get_json(url,datas)
        info_result = info_result+info
        #建立workbook,即excel
        workbook = xlwt.Workbook(encoding='utf-8')
        #建立表,第二引數用於確認同一個cell單元是否可以重設值
        worksheet = workbook.add_sheet('lagouzp',cell_overwrite_ok=True)
        for i, row in enumerate(info_result):
            # print row
            for j,col in enumerate(row):
                # print col
                time.sleep(1)
                worksheet.write(i,j,col)
        workbook.save('lagouzp.xls')

if __name__ == '__main__':
    main()


5-爬取結果如下