1.代码:
from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
import time
# 创建浏览器.
web = Chrome()
# 输入网址
web.get("https://www.lagou.com/")
# 找到那个x. 点击它
web.find_element_by_xpath('//*[@id="cboxClose"]').click()
time.sleep(1)
# web.find_element_by_xpath('//*[@id="search_input"]').send_keys("python", "回车")
web.find_element_by_xpath('//*[@id="search_input"]').send_keys("python", Keys.ENTER)
time.sleep(1)
web.find_element_by_xpath('/html/body/div[8]/div/div[2]').click()
f = open("招聘信息.txt", mode="a", encoding="utf-8")
# element apple
# elements apples
while 1:
li_lst = web.find_element_by_xpath('//*[@id="s_position_list"]/ul').find_elements_by_tag_name("li")
for li in li_lst:
# web.switch_to.window(web.window_handles[-1])
# web.close()
# web.switch_to.window(web.window_handles[0])
h3 = li.find_element_by_tag_name("h3")
title = h3.text
address = li.find_element_by_class_name("add").text
company_name = li.find_element_by_class_name('company_name').text
money = li.find_element_by_class_name('money').text
f.write(f"{title},{address},{company_name},{money}\n") # 为了后面如果有数据分析. 可以用pandas直接打开
web.find_element_by_xpath('//*[@id="s_position_list"]/div[2]/div/span[last()]').click()
print("下载好了一篇内容")
time.sleep(2)
2.生成招聘信息.TXT文件夹
3.打开记事本