-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExtract_Description_Indeed.py
More file actions
68 lines (57 loc) · 2.26 KB
/
Copy pathExtract_Description_Indeed.py
File metadata and controls
68 lines (57 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import pandas as pd
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import re
import time
# Set up the WebDriver service and options
service = Service()
options = webdriver.ChromeOptions()
options.add_argument("--incognito")
def fetch_description(url):
"""Fetch the job description from the given URL."""
driver = None
try:
# Initialize WebDriver
driver = webdriver.Chrome(service=service, options=options)
driver.get(url)
# Wait for the job description element to be present
wait = WebDriverWait(driver, 15)
try:
description_element = wait.until(
EC.presence_of_element_located((By.CLASS_NAME, 'jobsearch-JobComponent-description'))
)
# Get the text, clean it up, and return it
description = description_element.text.strip()
return re.sub(r'\s+', ' ', description)
except Exception as e:
print(f"Description element not found for {url}. Page source: {driver.page_source[:5000]}")
return None
except Exception as e:
print(f"Error fetching description from {url}: {e}")
return None
finally:
if driver:
driver.quit()
# Read input CSV file
input_csv_path = 'scraped_job_file.csv' # Path to your input CSV file
df = pd.read_csv(input_csv_path)
# List to store job descriptions
descriptions = []
# Process each job URL in the DataFrame
for index, row in df.iterrows():
job_id = row['Job ID']
url = row['URL']
description = fetch_description(url)
descriptions.append({'Job ID': job_id, 'Description': description})
time.sleep(1) # Optional: Add delay between requests
# Convert descriptions list to DataFrame
descriptions_df = pd.DataFrame(descriptions)
# Merge descriptions with the original DataFrame
merged_df = pd.merge(df, descriptions_df, on='Job ID', how='left')
# Save the updated DataFrame back to CSV
merged_df.to_csv(input_csv_path, index=False)
print(f"Job descriptions have been updated in {input_csv_path}")