selenium 和xpath爬拉鉤網招聘信息,數據寫入記事本

 

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.打開記事本

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章