python selenium自動化爬取Boss直聘崗位


環境准備

安裝selenium

查看chrome的版本

下載相應的chromedriver驅動

http://npm.taobao.org/mirrors/chromedriver/
http://chromedriver.storage.googleapis.com/index.html

然后將chromedriver放到python的安裝目錄即可

結構分析

觀察結構發現頁面url為:www.zhipin.com + 城市的拼音
https://www.zhipin.com/wuhan/

找到搜索框

找到提交按鈕

找到崗位詳情

代碼

from selenium import webdriver
import csv

fileName = "招聘數據.csv"
jobName = input("請輸入您的意願崗位:\n")
cityName = input("請輸入您的意願城市(拼音):\n")
url = f'https://www.zhipin.com/{cityName}/'
headerRow = [
    '崗位名稱',
    '工作地點',
    '起薪',
    '薪資區間',
    '學歷要求',
    '聯系人',
    '公司名稱',
    '業務范圍',
    '崗位職責/方向',
    '福利待遇',
]

def createTable():
    with open(fileName, 'a', encoding='utf-8-sig', newline='') as csvfile:
        csv_writer = csv.DictWriter(csvfile, fieldnames=headerRow)
        csv_writer.writeheader()  # 寫入表頭數據


def getOneJobPage(driver, writer):
    jobInfos = driver.find_elements_by_class_name('job-primary')  # 模擬點擊崗位詳情
    for info in jobInfos:  # 迭代取出數據
        print(info.text)  # 打印崗位詳情
        strText = info.text
        arrTxt = strText.split("\n")
        tmp = arrTxt[2].split("-")  # 將字符串變成數組
        arrTxt.insert(2, tmp[0])
        writer.writerow(arrTxt)  # 將數組寫入表格


def main():
    driver = webdriver.Chrome()
    driver.get(url)  # 獲取url
    searchEle = driver.find_element_by_class_name('ipt-search')  # 找到輸入框
    searchEle.send_keys(jobName)  # 輸入框中填上工作名稱
    btnSearch = driver.find_element_by_css_selector('button.btn.btn-search')
    btnSearch.click()  # 模擬點擊搜索按鈕

    pageS = 1
    while pageS < 50:  # page循環

        with open(fileName, 'a', encoding='utf-8-sig', newline='') as csvfile:
            # 打開文件csv文件
            writer = csv.writer(csvfile)

            getOneJobPage(driver, writer)  # 調用獲取崗位頁面函數
            pageS += 1
            driver.implicitly_wait(30)  # 隱式等待,30秒鍾內只要找到了元素就開始執行,未找到則超時;
            btnnext = driver.find_element_by_css_selector("a.next")
            # 尋找下一頁按鈕
            btnnext.click()  # 點擊按鈕

            # csvfile.close() #關閉文件

if __name__ == "__main__":
    createTable()
    main()

效果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM