51job_001

#!/usr/bin/python
# -*- coding: utf-8 -*-
from selenium import webdriver
import xlwt,csv,time
import os
# os.remove('51job爬虫.xls')
startup_Options = webdriver.ChromeOptions()
# 设置无界面模式运行浏览器
startup_Options.add_argument('--headless')
# 设置启动浏览器时窗口最大化运行
startup_Options.add_argument('--start-maximized')

driver = webdriver.Chrome(options=startup_Options)
driver.implicitly_wait(10)
driver.maximize_window()
driver.get('https://www.51job.com/')
driver.find_element_by_css_selector('#kwdselectid').send_keys('python')
driver.find_element_by_css_selector("div[class='ush top_wrap'] button").click()
webElements = driver.find_elements_by_css_selector('div.j_joblist > div.e')
'''
for循环实现列表推导式
for webElement in webElements:
    resutlist = []
    fields = webElement.find_elements_by_css_selector('span')
    for field in fields:
        resutlist.append(field.text)
    print(resutlist)
'''

for webElement in webElements:
    rows = []
    fields = webElement.find_elements_by_css_selector('span')
    resultlist = [field.text for field in fields]
    print(resultlist)

    # 列表推导式
    # resultlist = [field.text for field in fields]

    data = {
        "职位名称": resultlist[0],
        "发布日期": resultlist[1],
        "薪资待遇": resultlist[2],
        "工作要求": resultlist[3],
        "福利待遇": resultlist[4]
    }
    rows.append(data)
filepath = '51job招聘信息.csv'
with open(filepath,'w',newline='',encoding='utf-8') as f:
    f_csv = csv.DictWriter(f,fieldsnames=
                           ["职位名称",
                            "发布日期",
                            "薪资待遇",
                            "工作要求",
                            "福利待遇"
                           ]

    )
    f_csv.writeheader()
    f_csv.writerows(rows)


driver.quit()

 

posted @ 2021-07-03 22:36  Avicii_2018  阅读(147)  评论(0编辑  收藏  举报