forked from sigmachirality/indeed-resume-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindeed_scraper.py
239 lines (167 loc) · 5.35 KB
/
indeed_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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import urllib.request
import requests
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup
from multiprocessing.pool import ThreadPool
import time
import json
import threading
import traceback
from random import randint
from time import sleep
import os
class Thread2(Thread):
def __init__(self, group=None, target=None, name=None,
args=(), kwargs={}, Verbose=None):
Thread.__init__(self, group, target, name, args, kwargs)
self._return = None
def run(self):
print(type(self._target))
if self._target is not None:
self._return = self._target(*self._args,
**self._kwargs)
def join(self, *args):
Thread.join(self, *args)
return self._return
class CustomEncoder(json.JSONEncoder):
def default(self, z):
if isinstance(z, Resume):
return {"id": z.id, "jobs": z.jobs, "schools": z.schools}
elif isinstance(z, Job):
return {"title": z.title, "company": z.company, "location": z.location, "hire_date": z.hire_date}
elif isinstance(z, School):
return {"degree": z.degree, "school_name": z.school_name, "grad_date": z.grad_date}
else:
return super().default(z)
class Resume(object):
def __init__ (self, idd, jobs, schools):
self.id = idd
self.jobs = jobs
self.schools = schools
def toJSON(self):
return json.dumps(self, default=lambda o: o.__dict__,
sort_keys=True, indent=0)
class Job(object):
def __init__(self, title, company, location, hire_date):
self.title = title
self.company = company
self.location = location
self.hire_date = hire_date
class School(object):
def __init__(self, degree, school_name, grad_date):
self.degree = degree
self.school_name = school_name
self.grad_date = grad_date
def gen_idds(url, driver):
driver.get(url)
time.sleep(4) #wait for page to load
p_element = driver.page_source
soup = BeautifulSoup(p_element, "lxml")
links = soup.select(".icl-TextLink.icl-TextLink--primary.rezemp-u-h4")
idds=[]
for link in links:
path = link.get("href")
#print(path[8:path.find("?")])
idds.append(path[8:path.find("?")]) #8 is to account for "\resume\" at the beginning
return idds
#print(idds)
def gen_resume(idd, driver):
URL = '''https://resumes.indeed.com/resume/''' + idd
driver.get(URL)
p_element = driver.page_source
soup = BeautifulSoup(p_element, 'lxml')
results = soup.find_all('div', attrs={"class":"rezemp-ResumeDisplaySection"})
#print(results)
schools = []
try:
education = results[1]
content = education.find(class_="rezemp-ResumeDisplaySection-content")
for uni in content.children:
degree = uni.find(class_ = "rezemp-ResumeDisplay-itemTitle").get_text()
university = uni.find(class_="rezemp-ResumeDisplay-university").contents[0].get_text()
date = uni.find(class_="rezemp-ResumeDisplay-date").get_text()
schools.append(School(degree, university, date))
except:
pass
jobs = []
try:
work_experience = results[0]
job_titles = work_experience.find_all(class_="rezemp-u-h4")
job_descriptions = work_experience.find_all(class_="rezemp-u-h5")
for i in range(len(job_titles)):
dates = job_descriptions[i].find_next_sibling().get_text()
date = dates[:dates.find("to")]
title = job_titles[i].get_text()
desc = [p.get_text() for p in job_descriptions[i].find_all("span")][1:]
company = desc[0]
location = desc[1]
jobs.append(Job(title, company, location, date))
except:
pass
return Resume(idd, jobs, schools)
def mine(URL, override=True, rangee=None):
driver = webdriver.Chrome(os.path.dirname(os.path.abspath(__file__)) + os.sep + "chromedriver.exe")
driver.implicitly_wait(10)
if rangee == None:
start_index = 0 #700
target = 10901
else:
start_index = rangee[0]
target = rangee[1]
#print(start_index, target)
resumes = []
fail_ctr = 0
try:
while 1:
if (start_index >= target or fail_ctr >= 5):
break
stri = URL+"&start="+str(start_index)
idds = gen_idds(URL+"&start="+str(start_index), driver)
if (len(idds) == 0):
time.sleep(4) #wait a little bit, try again
fail_ctr += 1
continue
fail_ctr = 0
for idd in idds:
resumes.append(gen_resume(idd, driver))
start_index+=50
except Exception:
traceback.print_exc()
finally:
try:
driver.close()
except:
pass
return resumes
# resumes = [gen_resume(idd).toJSON() for idd in idds]
def mine_multi(url, override=True):
thread_list = []
names = []
resumes = []
pool = ThreadPool(processes=8)
async_result = pool.apply_async(mine, (url,)) # tuple of args for foo
#{"rangee": (0, 50)}
resumes = async_result.get() # get the return value from your function.
return resumes
def write_out_json(file_path, resumes):
print("Writing out data...")
with open(file_path, "w") as file:
json.dump(resumes, file, cls = CustomEncoder)
print("Done!")
def process_query(job, location, path = None):
file_name = "output_data_" + job + "_" + location + ".json"
if path is None:
path = os.path.dirname(os.path.abspath(__file__))
path += os.sep + file_name
URL = "https://resumes.indeed.com/search?q=" + job + "&l=" + location + "&searchFields="
resumes = mine_multi(URL)
write_out_json(path, resumes)
def main():
job = "data scientist"
location = "San Francisco"
process_query(job, location)
if __name__ == "__main__":
main()