-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
184 lines (141 loc) · 5.67 KB
/
main.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup
import time
import os
import datetime
import pandas as pd
import json
from createSheet import createSheetFromJson
from dataProcessing import dataProcessingFromKeywords
from uploadDrive import uploadToDrive
# Config
options = Options()
options.add_argument('-headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-gpu')
driver = webdriver.Firefox(options=options)
wait = WebDriverWait(driver, 3)
# Url (with short data for testing ) - https://in.indeed.com/jobs?q=html%2Ccss%2Cjavascript%2Creact%2Cnode+js%2Cjava%2Cfrontend%2Cbackend&l=India&sc=0kf%3Ajt%28new_grad%29%3B&fromage=1&vjk=6b3d8cb9cdcd3ace
# url (With Long Data) = 'https://in.indeed.com/jobs?q=html%2Ccss%2Cjs%2Cjsvascript%2Creact%2Cmongo%2Cnode%2Cui%2Cweb%2Creact+js%2Cjavascript+developer%2Cmern%2Creact+developer&l=India&sc=0kf%3Ajt%28fulltime%29%3B&fromage=1&vjk=383eb1d7dee2ebc6'
url = 'https://in.indeed.com/jobs?q=html%2Ccss%2Cjavascript%2Creact%2Cnode+js%2Cjava%2Cfrontend%2Cbackend&l=India&sc=0kf%3Ajt%28new_grad%29%3B&fromage=1&vjk=6b3d8cb9cdcd3ace'
driver.get(url)
# Main Data Array
job_array = []
# For Each page
while True:
try:
# Wait for modal and click close button
modal = wait.until(EC.presence_of_element_located((By.CLASS_NAME, "icl-CloseButton")))
modal.click()
except:
pass
# Get Html
html = driver.page_source
soup = BeautifulSoup(html, 'html.parser')
job_postings = soup.find_all('div', class_='job_seen_beacon')
# Get data for Each page
for job in job_postings:
job_data = {}
# Extract job title
job_title_h2 = job.find('h2', class_='jobTitle')
if job_title_h2:
job_data['jobTitle'] = job_title_h2.a.span.text.strip()
else:
job_data['jobTitle'] = "NA"
# Extract company name
company_name_span = job.find('span', class_='companyName')
if company_name_span:
job_data['companyName'] = company_name_span.text.strip()
else:
job_data['companyName'] = "NA"
# Extract company location
company_location_div = job.find('div', class_='companyLocation')
if company_location_div:
job_data['companyLocation'] = company_location_div.text.strip()
else:
job_data['companyLocation'] = "NA"
# Extract salary details
salary_div = job.find('div', class_='salary-snippet-container')
if salary_div:
# "\u20b9" -> "₹"
string = salary_div.div.text.strip()
salary = string.replace("\u20b9", "Rupee: ")
job_data['salaryDetails'] = salary
else:
job_data['salaryDetails'] = "NA"
# Extract job posting time
posted_time_span = job.find('span', class_='date')
if posted_time_span:
# PostedJust posted -> Posted : Just posted
s = posted_time_span.text.strip()
fixed_part = "Posted"
variable_part = s[len(fixed_part):].strip()
result = f"{fixed_part} : {variable_part}"
job_data['postedTime'] = result
else:
job_data['postedTime'] = "NA"
# Extract apply link
apply_link_a = job.find('a', class_='jcs-JobTitle')
if apply_link_a:
job_data['applyLink'] = 'https://in.indeed.com' + apply_link_a['href']
else:
job_data['applyLink'] = "NA"
# Extract other details
job_snippet_div = job.find('div', class_='job-snippet')
if job_snippet_div:
if job_snippet_div.ul:
other_details_array = job_snippet_div.ul.find_all('li')
if other_details_array:
other_details = " , ".join(
tag.string.strip() for tag in other_details_array if tag.string is not None)
job_data['otherDetails'] = other_details
else:
job_data['otherDetails'] = "NA"
else:
job_data['otherDetails'] = "NA"
else:
job_data['otherDetails'] = "NA"
# Appening all
job_array.append(job_data)
print("Done for a current page, Moving to next page...")
# print(job_array)
try:
# Click next page button
next_button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a[data-testid="pagination-page-next"]')))
next_button.click()
except:
# End of pagination
break
driver.quit()
print("Done All pages...")
# Sava all data into JSON Format
# Create a subdirectory named "json" if it doesn't exist
if not os.path.exists("json"):
os.makedirs("json")
# Generating File name
today = datetime.datetime.today()
todaysDate = today.strftime('%d/%m/%Y')
# Convert date string to datetime object
date = datetime.datetime.strptime(todaysDate, '%d/%m/%Y')
# Format output string
fileName = date.strftime('%dth-%b-') + "Indeed-posts"
# Build the file paths for the JSON and Excel files
fileNameJson = os.path.join("json", fileName + ".json")
# Write the JSON data to the file
with open(fileNameJson, 'w') as f:
json.dump(job_array, f)
# Load the JSON data into a DataFrame
with open(fileNameJson) as f:
data = pd.read_json(f)
print("Raw Json file Created ...")
# Data Processing
dataProcessingFromKeywords()
# Create one sheet
createSheetFromJson()
# Final Processed sheet Uploaded to Drive
uploadToDrive(fileName)