用Python批量爬取拉鉤網招聘信息

今天我們要爬取的是拉勾網的招聘信息

需求1:

  • 獲取以下信息
  1. 城市
  2. 公司名
  3. 公司規模
  4. 學歷
  5. 職位名稱
  6. 薪資
  7. 工作時間

需求2:

以逗號(,)分割信息內容,寫入csv文件。

網址分析:

  • URL :https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput=
  • 查看網址源碼


用Python批量爬取拉鉤網招聘信息

  • 通過查看源碼分析得知,拉勾網的數據都是動態加載的,需要通過抓包才能獲取裡面的數據。
  • 按F12或者右鍵點擊檢查找到以下頁面。(如果沒有請刷新一下頁面)

  • 用Python批量爬取拉鉤網招聘信息

    這麼多數據包,到底哪一個是我們需要的呢?

    搜索一下關鍵詞


    用Python批量爬取拉鉤網招聘信息

    確定裡面包含了我們需要的信息,那麼我們開始進行編寫爬蟲

    取出headers裡面的url

    <code>import requests 

    import json

    api_url ='https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false'
    header = {
    'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
    }
    response = requests.post(api_url)

    result = response.json()
    print(result)/<code>

    我們嘗試一下打印:

    用Python批量爬取拉鉤網招聘信息

    得到以下結果,提示操作太頻繁,但是隻請求了一次呀,請求頭也加了,怎麼會這樣呢?

    把以下幾個參數加入試試;


    用Python批量爬取拉鉤網招聘信息

  • Host
  • Origin
  • Referer
  • cookie
  • <code>import requests
    import json


    url ='https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false'
    header = {

    'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
    }

    response = requests.post(api_url,headers=header)
    result = response.json()
    print(result)/<code>

    打印以下結果看看


    用Python批量爬取拉鉤網招聘信息

    還是提示操作太頻繁了。user-agent和cookie都傳了,為什麼還是不行呢?

    分析一下:

    • 首先,我們的ip肯定是沒有被封的。
    • 根據http協議原理,cookie是http客戶端服務器設置的
    • js可以修改cookie
    • 那麼我們來清理一下所有的cookie


    用Python批量爬取拉鉤網招聘信息

    再次刷新網頁


    用Python批量爬取拉鉤網招聘信息

    服務器重新給我們返回了cookie,裡面包含著一些session,id等等信息

    那麼我們嘗試是一下那個是我們需要的:

    再次打印:


    用Python批量爬取拉鉤網招聘信息

    成功返回我們需要的數據~

    但是發現一個問題,這個cookie是不穩定的,使用的次數多了,還是會出現操作太頻繁的警告,那麼怎麼實現一勞永逸呢?

    向https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput=這個url發起請求。

    <code>url='https://www.lagou.com/jobs/list_python/p-city_0?&cl=false&fromSearch=true&labelWords=&suginput='
    responses = requests.get(url,headers={
    'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'
    })
    cookie = responses.cookies
    print(cookie)/<code>

    打印一下:


    用Python批量爬取拉鉤網招聘信息

    出現了我們需要的cookie,成功解決了我們剛才的不穩定因素。

    下面我們把cookie加入到請求裡面

    <code>url='https://www.lagou.com/jobs/list_python/p-city_0?&cl=false&fromSearch=true&labelWords=&suginput='
    responses = requests.get(url,headers={
    'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'
    })
    cookie = responses.cookies/<code>

    下面開始對數據進行處理


    用Python批量爬取拉鉤網招聘信息


    用Python批量爬取拉鉤網招聘信息

    通過返回的數據可以分析出,我們需要的數據在conment——positionResult——result這樣的層級裡面,下面我們開始編寫代碼。

    <code>import requests
    import json

    url='https://www.lagou.com/jobs/list_python/p-city_0?&cl=false&fromSearch=true&labelWords=&suginput='
    responses = requests.get(url,headers={
    'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'
    })
    cookie = responses.cookies


    api_url ='https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false'
    header = {
    'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
    \t\t'Host':'www.lagou.com'
    \t\t'Origin':'https://www.lagou.com'
    \t\t'Referer':'https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput='
    }
    data = {
    'first': 'true',
    'pn':'1',
    'kd':'python',
    }
    response = requests.post(api_url,headers=header,data=data,cookies=cookie) #發起請求
    result = response.json() #轉換成json格式的

    results = result['content']['positionResult']['result'] #通過層級獲取result裡面的信息
    for i in results:# 遍歷數據
    d = { #以字典的形式展示
    'city':i['city'],
    'companyFullName':i['companyFullName'],
    'companySize':i['companySize'],
    'education':i['education'],
    'positionName':i['positionName'],
    'salary':i['salary'],
    'workYear':i['workYear'],
    }
    \t\tprint('d')/<code>

    打印結果:


    用Python批量爬取拉鉤網招聘信息

    好啦,第一個需求已經完成,下面開始完成第二個需求;

    以逗號(,)分割信息內容,寫入csv文件。

    話不多說,開始編寫代碼

    <code>import requests
    import json

    url='https://www.lagou.com/jobs/list_python/p-city_0?&cl=false&fromSearch=true&labelWords=&suginput='
    responses = requests.get(url,headers={
    'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'
    })
    cookie = responses.cookies


    api_url ='https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false'
    header = {
    'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
    \t\t'Host':'www.lagou.com'
    \t\t'Origin':'https://www.lagou.com'
    \t\t'Referer':'https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput='
    }
    data = {
    'first': 'true',
    'pn':'1',
    'kd':'python',
    }
    response = requests.post(api_url,headers=header,data=data,cookies=cookie) #發起請求
    result = response.json() #轉換成json格式的

    results = result['content']['positionResult']['result'] #通過層級獲取result裡面的信息
    for i in results:# 遍歷數據
    d = { #以字典的形式展示
    'city':i['city'],
    'companyFullName':i['companyFullName'],
    'companySize':i['companySize'],
    'education':i['education'],

    'positionName':i['positionName'],
    'salary':i['salary'],
    'workYear':i['workYear'],
    }
    \t\twith open('拉鉤職位信息.csv','a',encoding='utf-8')as f:
    \t\tf.write(','.join(d.values()))
    f.write('\\n')/<code>

    運行一下


    用Python批量爬取拉鉤網招聘信息

    出現一個後綴為.csv的文件,我們打開看看吧。


    用Python批量爬取拉鉤網招聘信息

    大功告成了。

    總結

    1. 本章我們學習瞭如何實現應對反爬。
    1. 通過數據分析得知,一個數據包只有15條數據(參考數據處理那一段的圖片)那麼我們想要30,100條甚至更多呢?
    2. 注意:如果批量爬取,一定要設置延時,否則爬太快容易封ip,會導致一段時間無法進入該網站,同時也是很不友好的行為。
    3. 本文僅供學習交流,請勿商用。謝謝


    用Python批量爬取拉鉤網招聘信息


    分享到:


    相關文章: