python爬虫類--コードインスタンス(xpath)を這い出す

5393 ワード

Pythonは引き抜き網の例を這い出す
# -*-.coding: utf-8 -*-
# __author__ = 'xiaobai'
# Email: [email protected]
import time
import re
from selenium.webdriver import Chrome #        
from selenium.webdriver.common.keys import Keys
n = 1
#      
web = Chrome()
#      ,     
web.get("https://www.lagou.com")
web.find_element_by_xpath('//*[@id="cboxClose"]').click()

time.sleep(1)
#        ,  python,     
web.find_element_by_xpath('//*[@id="search_input"]').send_keys('python', Keys.ENTER)

alst = web.find_elements_by_class_name("position_link")
for a in alst:
    #   H3   
    a.find_element_by_tag_name("h3").click()
    web.switch_to.window(web.window_handles[-1])

    text= web.find_element_by_xpath('//*[@id="job_detail"]/dd[2]').text #    

    #            
    with open(f'  _{n}.txt', mode = 'w',encoding='utf-8') as f:
        f.write(text)
        n += 1

    #     
    web.close()

    #              
    web.switch_to.window(web.window_handles[0])
    time.sleep(1)