python自动化获取51job的职位

末鹿安然 提交于 2020-03-19 12:35:14

3 月,跳不动了?>>>

最近在学习python的自动化测试,跟着视频教程做了个利用chrome 驱动去招聘网站上获取了 成都python职位的相关信息。

请注意,不同的浏览器需要不同的驱动,chrome浏览器的驱动下载: https://chromedriver.storage.googleapis.com/index.html  ,请根据自己的浏览器版本下载对应的驱动。

简单说下,如何根据html css获取相应的内容:更多详细的教程移步: http://www.python3.vip/doc/tutorial/selenium/02/

find_elements_by_class_name  find_element_by_class_name 根据class样式名称选择元素find_elements_by_tag_name  根据标签选择元素find_element_by_id  根据id 选择元素
find_elements_by_css_selector 根据css样式选择

以下代码,只需要替换你自己的驱动路径,webdriver = webdriver.Chrome(r'你的驱动地址'),就可以立即跑起来了

# 自动化测试,爬取网站
import re

import xlwt as xlwt
from selenium import webdriver
import time

# 加载驱动
webdriver = webdriver.Chrome(r'e:\chromedriver.exe')
# 打开网址
webdriver.get("https://www.51job.com")
# 输入关键字
keyele = webdriver.find_element_by_id('kwdselectid')
keyele.send_keys('python')
# 地点选中
positionele = webdriver.find_element_by_id('work_position_input')
positionele.click()

time.sleep(1)
# 已经选中的地点
selecedEles = webdriver.find_elements_by_css_selector(
    '#work_position_click_center_right_list_000000 em[class=on]'
)

# 取消所有默认选中的地点
for ele in selecedEles:
    ele.click()
# 取消默认城市
# webdriver.find_element_by_id(
#     'work_position_click_ip_location_000000_090900').click()
# 选中成都
webdriver.find_element_by_id(
    'work_position_click_center_right_list_category_000000_090200').click()
# 确定
webdriver.find_element_by_id('work_position_click_bottom_save').click()

# 点击搜索
webdriver.find_element_by_css_selector('.ush button').click()

# 结果分析
time.sleep(2)
jobs = webdriver.find_elements_by_css_selector('#resultList div[class=el]')
# for job in jobs:
#     fields = job.find_elements_by_tag_name('span')
#     print(' | '.join([field.text for field in fields]))

book = xlwt.Workbook()
sh = book.add_sheet('统计结果')

# 写入
row = 0
for job in jobs:
    fields = job.find_elements_by_tag_name('span')
    col = 0
    for field in fields:
        text = field.text
        sh.write(row, col, text)
        col += 1
    row += 1
book.save('51job.xls')

# 退出驱动
webdriver.quit()

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!