-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjob_scraper_utils.py
220 lines (173 loc) · 7.4 KB
/
job_scraper_utils.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
import os
import smtplib
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import pandas as pd
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.common import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium_stealth import stealth
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
global total_jobs
def configure_webdriver():
options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=options)
stealth(driver,
languages=["en-US", "en"],
vendor="Google Inc.",
platform="Win32",
webgl_vendor="Intel Inc.",
renderer="Intel Iris OpenGL Engine",
fix_hairline=True,
)
return driver
def search_jobs(driver, country, job_position, job_location, date_posted):
full_url = f'{country}/jobs?q={"+".join(job_position.split())}&l={job_location}&fromage={date_posted}'
print(full_url)
driver.get(full_url)
global total_jobs
try:
job_count_element = driver.find_element(By.XPATH,
'//div[starts-with(@class, "jobsearch-JobCountAndSortPane-jobCount")]')
total_jobs = job_count_element.find_element(By.XPATH, './span').text
print(f"{total_jobs} found")
except NoSuchElementException:
print("No job count found")
total_jobs = "Unknown"
driver.save_screenshot('screenshot.png')
return full_url
def scrape_job_data(driver, country):
df = pd.DataFrame({'Link': [''], 'Job Title': [''], 'Company': [''],
'Employer Active': [''], 'Location': ['']})
job_count = 0
# count = 0
while True:
# count += 1
soup = BeautifulSoup(driver.page_source, 'lxml')
boxes = soup.find_all('div', class_='job_seen_beacon')
for i in boxes:
try:
link = i.find('a', {'data-jk': True}).get('href')
link_full = country + link
except (AttributeError, TypeError):
try:
link = i.find('a', class_=lambda x: x and 'JobTitle' in x).get('href')
link_full = country + link
except (AttributeError, TypeError):
link_full = None
try:
job_title = i.find('a', class_=lambda x: x and 'JobTitle' in x).text.strip()
except AttributeError:
try:
job_title = i.find('span', id=lambda x: x and 'jobTitle-' in str(x)).text.strip()
except AttributeError:
job_title = None
try:
company = i.find('span', {'data-testid': 'company-name'}).text.strip()
except AttributeError:
try:
company = i.find('span', class_=lambda x: x and 'company' in str(x).lower()).text.strip()
except AttributeError:
company = None
try:
employer_active = i.find('span', class_='date').text.strip()
except AttributeError:
try:
employer_active = i.find('span', {'data-testid': 'myJobsStateDate'}).text.strip()
except AttributeError:
employer_active = None
try:
location_element = i.find('div', {'data-testid': 'text-location'})
if location_element:
try:
location = location_element.find('span').text.strip()
except AttributeError:
location = location_element.text.strip()
else:
raise AttributeError
except AttributeError:
try:
location_element = i.find('div', class_=lambda x: x and 'location' in str(x).lower())
if location_element:
try:
location = location_element.find('span').text.strip()
except AttributeError:
location = location_element.text.strip()
else:
location = ''
except AttributeError:
location = ''
new_data = pd.DataFrame({'Link': [link_full], 'Job Title': [job_title],
'Company': [company],
'Employer Active': [employer_active],
'Location': [location]})
df = pd.concat([df, new_data], ignore_index=True)
job_count += 1
print(f"Scraped {job_count} of {total_jobs}")
try:
next_page = soup.find('a', {'aria-label': 'Next Page'}).get('href')
next_page = country + next_page
driver.get(next_page)
except:
break
return df
def clean_data(df):
def posted(x):
try:
x = x.replace('EmployerActive', '').strip()
return x
except AttributeError:
pass
df['Employer Active'] = df['Employer Active'].apply(posted)
return df
def save_csv(df, job_position, job_location):
def get_user_desktop_path():
home_dir = os.path.expanduser("~")
desktop_path = os.path.join(home_dir, "Desktop")
return desktop_path
file_path = os.path.join(get_user_desktop_path(), '{}_{}'.format(job_position, job_location))
csv_file = '{}.csv'.format(file_path)
df.to_csv('{}.csv'.format(file_path), index=False)
return csv_file
def send_email(df, sender_email, receiver_email, job_position, job_location, password):
sender = sender_email
receiver = receiver_email
password = password
msg = MIMEMultipart()
msg['Subject'] = 'New Jobs from Indeed'
msg['From'] = sender
msg['To'] = ','.join(receiver)
attachment_filename = generate_attachment_filename(job_position, job_location)
csv_content = df.to_csv(index=False).encode()
part = MIMEBase('application', 'octet-stream')
part.set_payload(csv_content)
encoders.encode_base64(part)
part.add_header('Content-Disposition', f'attachment; filename="{attachment_filename}"')
msg.attach(part)
s = smtplib.SMTP_SSL(host='smtp.gmail.com', port=465)
s.login(user=sender, password=password)
s.sendmail(sender, receiver, msg.as_string())
s.quit()
def send_email_empty(sender, receiver_email, subject, body, password):
msg = MIMEMultipart()
password = password
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = ','.join(receiver_email)
# Attach the body as the text/plain part of the email
msg.attach(MIMEText(body, 'plain'))
s = smtplib.SMTP_SSL(host='smtp.gmail.com', port=465)
s.login(user=sender, password=password)
s.sendmail(sender, receiver_email, msg.as_string())
s.quit()
def generate_attachment_filename(job_title, job_location):
filename = f"{job_title.replace(' ', '_')}_{job_location.replace(' ', '_')}.csv"
return filename