天天看點

python selenium 爬取網頁審查元素_Python 爬蟲 | selenium爬取某招聘平台

最近小編參加了學校的爬蟲比賽,由于我是比賽開始後一天我才知道有這個比賽,這個比賽不止需要做爬資料,還需要做資料分析,是以時間比較緊。

本次比賽的主題是圍繞着大資料工程師進行資料的爬取和分析。

本次的受害者

aHR0cHMlM2ElMmYlMmZ3d3cubGFnb3UuY29tL2pvYnMvbGlzdF8lRTUlQTQlQTclRTYlOTUlQjAlRTYlOEQlQUU/bGFiZWxXb3Jkcz0mZnJvbVNlYXJjaD10cnVlJnN1Z2lucHV0PQ==

目錄:

1、環境

2、分析網站

3、實作爬取

1、環境

Python3.7、requests、selenium

2、分析網站

先打開開發者工具,再在搜尋欄中輸入大資料

python selenium 爬取網頁審查元素_Python 爬蟲 | selenium爬取某招聘平台

圖1

python selenium 爬取網頁審查元素_Python 爬蟲 | selenium爬取某招聘平台

圖2

篩選XHR請求很容易就可以發現目标資料所在的請求如圖3

python selenium 爬取網頁審查元素_Python 爬蟲 | selenium爬取某招聘平台

圖3

找到了請求,第二步就是分析請求是否有加密,我的習慣是直接看最下面的請求參數

python selenium 爬取網頁審查元素_Python 爬蟲 | selenium爬取某招聘平台

圖4

很明顯沒有加密參數,直接請求這個資料的接口,得到如圖5,沒有得到資料,肯定有什麼地方是加密了的,估計就是cookie加密了

python selenium 爬取網頁審查元素_Python 爬蟲 | selenium爬取某招聘平台

圖5

看一下cookie,如圖6

python selenium 爬取網頁審查元素_Python 爬蟲 | selenium爬取某招聘平台

圖6

我看了一下背後的邏輯,是經過混淆的如圖7

由于比賽時間比較緊,我就直接用selenium擷取cookie,拿到資料進行資料分析再說。

python selenium 爬取網頁審查元素_Python 爬蟲 | selenium爬取某招聘平台

圖7

3、實作爬取

用selenium爬取就基本沒有難點,用selenium運作js代碼得到cookie即可

def get_cookie(url):
    browser.get(url)
    js = 'var c = document.cookie;return c'  # js語句
    return browser.execute_script(js)  # 執行js
           

這裡是post請求,需要改變參數已達到請求不同頁碼的資料

def get_json(content):
    a = content.get('content').get('positionResult').get('result')
    city, jod_year, experience, XL, ave_money, business = [], [], [], [], [], []
    for i in a:
        city.append(i.get('city'))
        jod_year.append(i.get('workYear'))
        XL.append(i.get('education'))
        ave_money.append((i.get('salary')))
        business.append(i.get('industryField'))
    for i in range(len(city)):
        data = {'ave_money': ave_money[i], 'HY': business[i], 'job_year': jod_year[i], 'XL': XL[i], 'zone': city[i]}
        print(data)

def main():
    cookie = get_cookie(url=goal_url)
    headers['cookie'] = cookie
    for i in range(1):
        data = {
            'first': 'true',
            'pn': f'{i+1}',
            'kd': 'python'
        }
        # allow_redirects=True, verify=False
        response = requests.post(url='https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false',
                                 headers=headers, data=data)
        # 休眠随機時間
        sleep_time = random.uniform(0, 5)
        time.sleep(sleep_time)
        get_json(response.json())
           
python selenium 爬取網頁審查元素_Python 爬蟲 | selenium爬取某招聘平台

圖8

我需要的資料:工資、工資經驗、地區、學曆、行業,構成字典直接存入MongoDB中

def get_json(content):
    a = content.get('content').get('positionResult').get('result')
    city, jod_year, experience, XL, ave_money, business = [], [], [], [], [], []
    for i in a:
        city.append(i.get('city'))
        jod_year.append(i.get('workYear'))
        XL.append(i.get('education'))
        ave_money.append((i.get('salary')))
        business.append(i.get('industryField'))
    for i in range(len(city)):
        data = {'ave_money': ave_money[i], 'HY': business[i], 'job_year': jod_year[i], 'XL': XL[i], 'zone': city[i]}
        print(data)
           

在這挖個坑,之後我會更新如何逆向出cookie的生成邏輯。