|
| 1 | +from selenium import webdriver |
| 2 | +from selenium.webdriver.common.by import By |
| 3 | +from selenium.webdriver.chrome.options import Options |
| 4 | +from selenium.webdriver.support.ui import WebDriverWait |
| 5 | +from selenium.webdriver.support import expected_conditions as EC |
| 6 | +import time |
| 7 | +import pandas as pd |
| 8 | + |
| 9 | +# 크롬 설정 |
| 10 | +options = Options() |
| 11 | +options.add_experimental_option("detach", True) |
| 12 | +driver = webdriver.Chrome(options=options) |
| 13 | +driver.set_window_size(1280, 1024) |
| 14 | +wait = WebDriverWait(driver, 20) |
| 15 | + |
| 16 | +# 사이트 접속 |
| 17 | +driver.get("https://zighang.com/it") |
| 18 | +time.sleep(2) |
| 19 | + |
| 20 | +# 직무 설정 |
| 21 | +job_name = "서버·백엔드" |
| 22 | + |
| 23 | +# 직무 필터 열기 |
| 24 | +arrow_xpath = '//*[@id="root"]/main/div[3]/div/div/div/div/div[2]/div/section/button[2]/div/img' |
| 25 | +wait.until(EC.element_to_be_clickable((By.XPATH, arrow_xpath))).click() |
| 26 | +print("직무 필터 열기 성공") |
| 27 | +time.sleep(1) |
| 28 | + |
| 29 | +# 직무 클릭 |
| 30 | +job_button_xpath = f'//button[normalize-space()="{job_name}"]' |
| 31 | +wait.until(EC.element_to_be_clickable((By.XPATH, job_button_xpath))).click() |
| 32 | +print(f" 직무 '{job_name}' 선택 완료") |
| 33 | +time.sleep(1) |
| 34 | + |
| 35 | +# 공고 보기 클릭 |
| 36 | +confirm_button = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div.sticky.bottom-0 button.bg-primary'))) |
| 37 | +driver.execute_script("arguments[0].scrollIntoView(true);", confirm_button) |
| 38 | +driver.execute_script("arguments[0].click();", confirm_button) |
| 39 | +print("공고 보기 버튼 클릭 완료") |
| 40 | +wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'p.ds-web-title2'))) |
| 41 | +print("공고 리스트 로딩 완료") |
| 42 | + |
| 43 | +# 공고 순회 |
| 44 | +MAX_CLICKS = 10 |
| 45 | +original_tab = driver.current_window_handle |
| 46 | +results = [] |
| 47 | + |
| 48 | +for i in range(2, MAX_CLICKS + 1): |
| 49 | + try: |
| 50 | + title_xpath = f'(//p[contains(@class, "ds-web-title2")])[{i}]' |
| 51 | + title_elem = wait.until(EC.presence_of_element_located((By.XPATH, title_xpath))) |
| 52 | + driver.execute_script("arguments[0].scrollIntoView(true);", title_elem) |
| 53 | + driver.execute_script("window.scrollBy(0, -200);") |
| 54 | + time.sleep(0.3) |
| 55 | + parent_link = title_elem.find_element(By.XPATH, "./ancestor::a[1]") |
| 56 | + driver.execute_script("arguments[0].click();", parent_link) |
| 57 | + print(f"▶️ [{i}]번째 공고 클릭 → 새 탭 열림 예상") |
| 58 | + time.sleep(2) |
| 59 | + |
| 60 | + # 새 탭 전환 |
| 61 | + new_tab = [tab for tab in driver.window_handles if tab != original_tab][0] |
| 62 | + driver.switch_to.window(new_tab) |
| 63 | + |
| 64 | + # 공고 정보 크롤링 |
| 65 | + data = {} |
| 66 | + data["회사명"] = driver.find_element(By.XPATH, '//*[@id="root"]/main/div[2]/div[1]/div[1]/div[1]/div[1]/div/a').text |
| 67 | + data["경력"] = driver.find_element(By.XPATH, '//*[@id="root"]/main/div[2]/div[1]/div[1]/div[1]/div[5]/div/section/div[1]/div/div').text |
| 68 | + data["학력"] = driver.find_element(By.XPATH, '//*[@id="root"]/main/div[2]/div[1]/div[1]/div[1]/div[5]/div/section/div[3]/div/div').text |
| 69 | + data["근무지"] = driver.find_element(By.XPATH, '//*[@id="root"]/main/div[2]/div[1]/div[1]/div[1]/div[5]/div/section/div[2]/div/div').text |
| 70 | + data["직군"] = job_name |
| 71 | + |
| 72 | + try: |
| 73 | + data["우대사항"] = driver.find_element(By.XPATH, '//h2[text()="우대사항"]/following-sibling::p').text |
| 74 | + print(f"우대사항 크롤링 완료: {data['우대사항'][:10]}...") |
| 75 | + except: |
| 76 | + data["우대사항"] = "" |
| 77 | + |
| 78 | + try: |
| 79 | + data["자격요건"] = driver.find_element(By.XPATH, '//h2[text()="자격요건"]/following-sibling::p').text |
| 80 | + print(f"자격요건 크롤링 완료: {data['자격요건'][:10]}...") |
| 81 | + except: |
| 82 | + data["자격요건"] = "" |
| 83 | + |
| 84 | + if data["우대사항"] == "" and data["자격요건"] == "": |
| 85 | + try: |
| 86 | + img_elem = driver.find_element(By.XPATH, '//*[@id="root"]/main/div[2]/div[1]/div[1]/div[4]/img') |
| 87 | + data["이미지경로"] = img_elem.get_attribute("src") |
| 88 | + print("이미지 URL 저장 완료") |
| 89 | + except: |
| 90 | + data["이미지경로"] = "" |
| 91 | + print("이미지 URL 저장 실패") |
| 92 | + else: |
| 93 | + data["이미지경로"] = "" |
| 94 | + |
| 95 | + results.append(data) |
| 96 | + driver.close() |
| 97 | + driver.switch_to.window(original_tab) |
| 98 | + print(f"🔙 기존 탭 복귀 완료\n") |
| 99 | + |
| 100 | + except Exception as e: |
| 101 | + print(f"[{i}]번째 공고 실패: {e}") |
| 102 | + continue |
| 103 | + |
| 104 | +# 엑셀 저장 |
| 105 | +df = pd.DataFrame(results) |
| 106 | +df.to_excel("직행_크롤링_결과.xlsx", index=False) |
| 107 | +print("엑셀 저장 완료") |
0 commit comments