-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglassdoor_scraper.py
207 lines (176 loc) · 10.5 KB
/
glassdoor_scraper.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
from selenium.common.exceptions import NoSuchElementException, ElementClickInterceptedException
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
import time
import pandas as pd
def get_jobs(keyword, num_jobs, verbose, path, sleep_time):
'''Gathers jobs as a dataframe, scraped from Glassdoor'''
#Initializing the webdriver
options = webdriver.ChromeOptions()
options.add_argument("--disable-notifications")
#Uncomment the line below if you'd like to scrape without a new Chrome window every time.
# options.add_argument('headless')
#Change the path to where chromedriver is in your home folder.
driver = webdriver.Chrome(executable_path=path, options=options)
driver.set_window_size(1120, 1000)
url = 'https://www.glassdoor.co.in/Job/' + keyword + '-jobs-SRCH_KO0,14.htm?suggestCount=0&suggestChosen=false&clickSource=searchBtn&typedKeyword=&typedLocation=&context=Jobs&dropdown=0'
driver.get(url)
jobs = []
while len(jobs) < num_jobs: #If true, should be still looking for new jobs.
done = False
while not done:
#Going through each job in this page
job_buttons = driver.find_elements(By.XPATH,".//article[@id='MainCol']//ul/li[@data-test='jobListing']") #These are the buttons we're going to click.
for job_button in job_buttons:
#Let the page load. Change this number based on your internet speed.
# Or, wait until the webpage is loaded, instead of hardcoding it.
# time.sleep(sleep_time)
#Test for the "Sign Up" prompt and get rid of it.
##---------------- Simpler code to close the pop-up prompts-------##
try:
signup_element = driver.find_element(By.XPATH,".//div[@class='background-overlay']//span[@alt='Close']")
WebDriverWait(driver,sleep_time).until(EC.presence_of_element_located(signup_element)).click()
# driver.execute_script("arguments[0].click();",signup_element)
except:
NoSuchElementException
#To Click "Refresh Page Button"
# try:
# element_1 = driver.find_element(By.XPATH,".//div[@id='JDCol']//button[@type='button']")
# driver.execute_script("arguments[0].click()",element_1)
# except:
# NoSuchElementException
time.sleep(0.1)
##---------------------------- Print the Progess of each job --------------------------##
print("Progress: {}".format("" + str(len(jobs)) + "/" + str(num_jobs)))
if len(jobs) >= num_jobs:
break
##------------------------------ Click each job posting ------------------------------##
# try:
# WebDriverWait(driver, 5).until(EC.invisibility_of_element_located((By.XPATH, ".//div[@class='background-overlay']")))
target_element = WebDriverWait(driver,1).until(EC.element_to_be_clickable(job_button))
ActionChains(driver).move_to_element(target_element).click().perform()
time.sleep(1)
# except:
# if ElementClickInterceptedException:
# actions = ActionChains(driver)
# actions.move_to_element(job_button).click().perform()
#To Click "Refresh Page Button"
try:
element_1 = driver.find_element(By.XPATH,".//div[@id='JDCol']//button[@type='button']")
driver.execute_script("arguments[0].click()",element_1)
except:
NoSuchElementException
##-------------------------- Collect necessary data from each job posting --------------------------##
collected_successfully = False
while not collected_successfully:
try:
company_name = driver.find_element(By.XPATH,".//div[@data-test='employerName']").text
location = driver.find_element(By.XPATH,".//div[@data-test='location']").text
job_title = driver.find_element(By.XPATH,".//div[@data-test='jobTitle']").text
# click on the "Show more" button
show_more_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, ".//div[@class='css-t3xrds e856ufb4']")))
show_more_button.click()
# wait for the text to load
element_with_text = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, ".//div[@id='JobDescriptionContainer']")))
job_description = element_with_text.text
except:
time.sleep(5)
try:
salary_estimate = driver.find_element(By.XPATH,".//div[@class='css-w04er4 e1tk4kwz6']//span[@data-test='detailSalary']").text
except NoSuchElementException:
salary_estimate = -1 #You need to set a "not found value. It's important."
try:
rating = driver.find_element(By.XPATH,'.//span[@data-test="detailRating"]').text
except NoSuchElementException:
rating = -1 #You need to set a "not found value. It's important."
# Printing for debugging
if verbose:
print("Job Title: {}".format(job_title))
print("Salary Estimate: {}".format(salary_estimate))
print("Job Description: {}".format(job_description[:500]))
# print("Rating: {}".format(rating))
print("Company Name: {}".format(company_name))
print("Location: {}".format(location))
#Going to the Company tab...
#clicking on this:
#<div class="tab" data-tab-type="overview"><span>Company</span></div>
try:
# # #<div class="infoEntity">
# # # <label>Headquarters</label>
# # # <span class="value">San Francisco, CA</span>
# # #</div>
headquarters = driver.find_element(By.XPATH,'.//div[@id="CompanyContainer"]//span[text()="Headquarters"]//following-sibling::*').text
except NoSuchElementException:
headquarters = -1
try:
size = driver.find_element(By.XPATH,'.//div[@id="CompanyContainer"]//span[text()="size"]//following-sibling::*').text
except NoSuchElementException:
size = -1
try:
founded = driver.find_element(By.XPATH,'.//div[@id="CompanyContainer"]//span[text()="Founded"]//following-sibling::*').text
except NoSuchElementException:
founded = -1
try:
type_of_ownership = driver.find_element(By.XPATH,'.//div[@id="CompanyContainer"]//span[text()="Type"]//following-sibling::*').text
except NoSuchElementException:
type_of_ownership = -1
try:
industry = driver.find_element(By.XPATH,'//div[@id="CompanyContainer"]//span[text()="Industry"]//following-sibling::*').text
except NoSuchElementException:
industry = -1
try:
sector = driver.find_element(By.XPATH,'.//div[@id="CompanyContainer"]//span[text()="Sector"]//following-sibling::*').text
except NoSuchElementException:
sector = -1
try:
revenue = driver.find_element(By.XPATH,'.//div[@id="CompanyContainer"]//span[text()="Revenue"]//following-sibling::*').text
except NoSuchElementException:
revenue = -1
try:
competitors = driver.find_element(By.XPATH,'.//div[@id="CompanyContainer"]//span[text()="Competitors"]//following-sibling::*').text
except NoSuchElementException:
competitors = -1
if verbose:
print("Headquarters: {}".format(headquarters))
print("Size: {}".format(size))
print("Founded: {}".format(founded))
print("Type of Ownership: {}".format(type_of_ownership))
print("Industry: {}".format(industry))
print("Sector: {}".format(sector))
print("Revenue: {}".format(revenue))
print("Competitors: {}".format(competitors))
print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
##--------------------------------- Store every collected data as a dictionary --------------------##
jobs.append({"Job Title" : job_title,
"Salary Estimate" : salary_estimate,
"Job Description" : job_description,
"Rating" : rating,
"Company Name" : company_name,
"Location" : location,
"Headquarters" : headquarters,
"Size" : size,
"Founded" : founded,
"Type of ownership" : type_of_ownership,
"Industry" : industry,
"Sector" : sector,
"Revenue" : revenue,
"Competitors" : competitors
})
#add job to jobs
done = True
##-------------------- Move to next page after clicking all job posting in a page ---------------##
#Clicking on the "next page" button
if done:
try:
element = driver.find_element(By.XPATH,".//span[@alt='Close']")
driver.execute_script("arguments[0].click();",element)
except:
NoSuchElementException
driver.find_element(By.XPATH,"//span[@alt='next-icon']").click()
time.sleep(sleep_time)
else:
break
return pd.DataFrame(jobs) #This line converts the dictionary object into a pandas DataFrame.