-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape.py
168 lines (141 loc) · 5.52 KB
/
scrape.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
from bs4 import BeautifulSoup
import requests
from random import random
from time import sleep
from email.message import EmailMessage
from collections import namedtuple
import smtplib
import csv
EmailCredentials = namedtuple("EmailCredentials", ['username', 'password', 'sender', 'recipient'])
def generate_url(job_title, job_location):
url_template = "https://www.indeed.com/jobs?q={}&l={}"
url = url_template.format(job_title, job_location)
return url
def save_record_to_csv(record, filepath, create_new_file=False):
"""Save an individual record to file; set `new_file` flag to `True` to generate new file"""
header = ["JobTitle", "Company", "Location", "Salary", "PostDate", "Summary", "JobUrl"]
if create_new_file:
with open(filepath, mode='w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow(header)
else:
with open(filepath, mode='a+', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow(record)
def email_jobs_file(filepath, email):
"""This is currently setup for GMAIL. However, you may need to enable `less secure apps` for
your email account if you want this to work. See: https://support.google.com/accounts/answer/6010255?hl=en"""
smtp_host = 'smtp.gmail.com'
smtp_port = 587
with smtplib.SMTP(host=smtp_host, port=smtp_port) as server:
server.starttls()
server.login(email.username, email.password)
message = EmailMessage()
message['From'] = email.sender
message['To'] = email.recipient
message['Subject'] = "Updated jobs file"
message['Body'] = "The updated Indeed postings are attached."
message.add_attachment(open(filepath, 'r').read(), filename="indeed.csv")
server.send_message(message)
def collect_job_cards_from_page(html):
soup = BeautifulSoup(html, 'html.parser')
cards = soup.find_all('div', 'job_seen_beacon')
return cards, soup
def sleep_for_random_interval():
seconds = random() * 10
sleep(seconds)
def request_jobs_from_indeed(url):
headers = {
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,'
'application/signed-exchange;v=b3;q=0.9',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.9',
'cache-control': 'max-age=0',
'sec-fetch-dest': 'document',
'sec-fetch-mode': 'navigate',
'sec-fetch-site': 'none',
'sec-fetch-user': '?1',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.47 '
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.text
else:
return None
def find_next_page(soup):
try:
pagination = soup.find("a", {"aria-label": "Next"}).get("href")
return "https://www.indeed.com" + pagination
except AttributeError:
return None
def extract_job_card_data(card):
atag = card.h2.a.span
link = card.h2.a
try:
job_title = atag.get('title')
except AttributeError:
job_title = ''
try:
company=card.find('span', 'companyName').text.strip()
except AttributeError:
company = ''
try:
location = card.find('div', 'companyLocation').text
except AttributeError:
location = ''
try:
job_summary = card.find('div', 'job-snippet').text.strip().replace('\n',' ')
except AttributeError:
job_summary = ''
try:
post_date = card.find('span', 'date').text[6:]
except AttributeError:
post_date = ''
try:
salary = card.find('span', 'estimated-salary').text[10:]
except AttributeError:
salary = ''
job_url = 'https://www.indeed.com' + link.get('href')
return job_title, company, location, job_summary, salary, post_date, job_url
def main(job_title, job_location, filepath, email=None):
unique_jobs = set() # track job urls to avoid collecting duplicate records
print("Starting to scrape indeed for `{}` in `{}`".format(job_title, job_location))
url = generate_url(job_title, job_location)
save_record_to_csv(None, filepath, create_new_file=True)
while True:
print(url)
html = request_jobs_from_indeed(url)
if not html:
break
cards, soup = collect_job_cards_from_page(html)
for card in cards:
record = extract_job_card_data(card)
if not record[-1] in unique_jobs:
save_record_to_csv(record, filepath)
unique_jobs.add(record[-1])
sleep_for_random_interval()
url = find_next_page(soup)
if not url:
break
print('Finished collecting {:,d} job postings.'.format(len(unique_jobs)))
if email:
email_jobs_file(filepath, email)
if __name__ == '__main__':
# job search settings
title = 'Data Scientist'
loc = 'Charlotte NC'
path = 'data_scientist_jobs.csv'
# include email settings if you want to email the file
# currently setup for GMAIL... see notes above.
email_settings = EmailCredentials(
username='EMAIL ID HERE',
password='PASSWORD HERE',
sender='[email protected]',
recipient='[email protected]'
)
# using email settings
# main(title, loc, path, email_settings)
# without email settings
main(title, loc, path)