python_selenium智联搜索
去招聘会工作,奈何网上仅仅提供招聘会的公司名字,没有提供招聘的职位,SO我写了个小代码给别人在智联上面搜索职位,由于时间紧迫,前程的就不写了
代码语言:javascript复制#!/usr/bin/python3.4
# -*- coding: utf-8 -*-
from selenium import webdriver
from lxml import etree
import time
# 打开浏览器
def openbrowser(keyword):
global browser
# 打开谷歌浏览器
# Firefox()
# Chrome()
# browser = webdriver.Chrome()
options = webdriver.ChromeOptions()
prefs = {
'profile.default_content_setting_values': {
'images': 2
}
}
options.add_experimental_option('prefs', prefs)
browser = webdriver.Chrome(chrome_options=options)
try:
browser.find_element_by_id("KeyWord_kw2").send_keys(keyword)
browser.find_element_by_class_name("doSearch").click()
time.sleep(5)
htmlcontent = browser.page_source
browser.quit()
except:
url = "http://www.zhaopin.com/"
browser.get(url)
# 打开浏览器时间
# print("等待10秒打开浏览器...")
# time.sleep(10)
browser.find_element_by_id("KeyWord_kw2").send_keys(keyword)
browser.find_element_by_class_name("doSearch").click()
time.sleep(5)
htmlcontent = browser.page_source
browser.quit()
return htmlcontent
def analyzehtml(html):
info = []
page = etree.HTML(html.lower())
hrefs = page.xpath('//a[@style="font-weight: bold"]/text()')
for href in hrefs:
info.append(href)
return info
if __name__ == "__main__":
file = open("../txt/company.txt")
companys = file.readlines()
for keyword in companys:
print(keyword.strip())
html = openbrowser(keyword.strip())
a = analyzehtml(html)
a.insert(0, str(keyword.strip()))
print(a)
b = "&&".join(a)
if len(b)==0:
b = "智联没有找到该公司"
filewrite = open("../txt/qwe.txt", "a")
filewrite.write("n" str(b))
filewrite.close()