-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
114 lines (105 loc) · 3.91 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
import requests
import time
import json
import datetime as dt
import csv
def formatDate(date):
date = dt.datetime.strptime(date, '%Y-%m-%dT%H:%M:%S%z')
return date.strftime('%d-%m-%Y')
def getSalary(salary):
if salary:
if salary['from'] and salary['to']:
return f"{salary['from']} - {salary['to']} {salary['currency']}"
elif salary['from']:
return f"{salary['from']} {salary['currency']}"
return None
def getPage(keyword, page = 0, country_code = 40):
params = {
'text': f'NAME:{keyword}', # Job title keyword
'area': country_code, # Kazakhstan
'page': page,
'per_page': 100
}
req = requests.get('https://api.hh.ru/vacancies', params)
if req.status_code == 200:
data = req.content.decode() # Decode for cyrillic symbols
req.close()
return data
else:
req.close()
return None
def exploreVacancies(keyword, max_pages = 20, time_sleep = 0.5, country_code = 40):
try:
for page in range(0, max_pages):
data = getPage(keyword, page, country_code)
if data is None:
break
data = json.loads(data)
for item in data['items']:
print(f"""
Title: {item['name']},
Salary: {getSalary(item['salary'])},
City: {item['area']['name']},
Job: {item['employer']['name']},
Publish Date: {formatDate(item['published_at'])},
Requirements: {item['snippet']['requirement']},
Responsibilites: {item['snippet']['responsibility']},
Schedule: {item['schedule']},
Experience: {item['experience']['name']},
Employment: {item['employment']['name']},
URL: {item['alternate_url']}
""")
print('====================')
time.sleep(time_sleep)
except Exception as e:
print('Something went wrong! Error: ', e)
def exportAsCSV(keyword, max_pages = 20, time_sleep = 0.5, country_code = 40):
try:
with open(f'data/{keyword}.csv', mode='w+', encoding='utf-8', newline="") as file:
writer = csv.writer(file)
writer.writerow(['title', 'salary', 'city', 'job', 'publish_date', 'requirements', 'responsibilities', 'schedule', 'experience', 'employment', 'url'])
for page in range(0, max_pages):
data = getPage(keyword, page, country_code)
if data is None:
break
data = json.loads(data)
for item in data['items']:
writer.writerow([item['name'], getSalary(item['salary']), item['area']['name'], item['employer']['name'], formatDate(item['published_at']), item['snippet']['requirement'], item['snippet']['responsibility'], item['schedule'], item['experience']['name'], item['employment']['name'], item['alternate_url']])
time.sleep(time_sleep)
except Exception as e:
print('Something went wrong! Error: ', e)
def main():
country_codes = {
"UA": 5,
"AZ": 9,
"BY": 16,
"GE": 28,
"KZ": 40,
"KG": 48,
"UZ": 97,
"RU": 113,
"Other": 1001
}
print("Head Hunter Vacancies Explorer: ")
print("----------")
# Ukraine: UA
# Azerbaijan: AZ
# Belarus: BY
# Georgia: GE
# Kazakhstan: KZ
# Kyrgyzstan: KG
# Uzbekistan: UZ
# Russia: RU
# Other: NA
country = input('Enter a country code (ex.: KZ): ')
if country not in country_codes:
print('Try Again!')
exit()
else:
country_code = country_codes[country]
keyword = input('Enter a job keyword: ')
exploreVacancies(keyword, country_code)
exportAsCSV(keyword, country_code)
print('Done!')
if __name__ == '__main__':
main()