-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwanted-crawler.py
271 lines (234 loc) Β· 11.9 KB
/
wanted-crawler.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import requests
from bs4 import BeautifulSoup
from webConnection import connectWebDriver, scrollPage
from contextlib import closing
from multiprocessing import Pool, Manager
from itertools import repeat
import re
from fileIO import openJsonFile, closeJsonFile, saveError
from dbIO import readDB, insertDB
import nltk
from nltk.corpus import stopwords
from konlpy.tag import Okt
from ckonlpy.tag import Twitter, Postprocessor
from ckonlpy.utils import load_wordset, load_ngram
# nltk.download('punkt')
# nltk.download('stopwords')
okt = Okt()
twitter = Twitter()
stopwordsKR = load_wordset('cleansing_data/korean_stopwords.txt', encoding='ANSI')
customStopwordsEN = load_wordset('cleansing_data/english_stopwords.txt', encoding='ANSI')
stopwordsEN = customStopwordsEN.union(set(stopwords.words('english')))
ngrams = load_ngram('cleansing_data/korean_ngram.txt')
userdicts = load_wordset('cleansing_data/korean_user_dict.txt')
twitter.add_dictionary(list(userdicts), 'Noun', force=True)
def getJobGroups():
res = requests.get(
'https://www.wanted.co.kr/wdlist/518?country=kr&job_sort=job.latest_order&years=-1&locations=all')
html = res.text
soup = BeautifulSoup(html, "html.parser")
jobGroups = []
for elements in soup.find("div", class_="_2h5Qtv_8mK2LOH-yR3FTRs").find_all("li"):
href = elements.find("a")["href"]
span = elements.find("span")
jobGroup = {'jobGroup': span.get_text(), 'url': "https://www.wanted.co.kr" + href}
jobGroups.append(jobGroup)
print(jobGroup)
insertDB("jobGroup", jobGroups)
return jobGroups
def getRecruitInfoList(urlDict, recruitInfos):
print(urlDict['jobGroup'])
driver = connectWebDriver(urlDict['url'])
scrollPage(driver)
allRecruitInfo = driver.find_elements_by_xpath('//div[@class="_3D4OeuZHyGXN7wwibRM5BJ"]/a')
if allRecruitInfo:
for recruitInfo in allRecruitInfo:
jobGroup = urlDict['jobGroup']
recruitInfoUrl = recruitInfo.get_attribute('href')
recruitInfo = {'jobGroup': jobGroup, 'url': recruitInfoUrl}
recruitInfos.append(recruitInfo)
# with open('data/recruitInfoList.csv', 'a', encoding='utf-8-sig', newline='') as file:
# writer = csv.writer(file)
# writer.writerow([jobGroup, recruitInfoUrl])
driver.quit()
def scrapRecruitList(groups):
# origin_headers = ['μ§κ΅°', 'url']
# with open('data/recruitInfoList.csv', 'w', encoding='utf-8-sig', newline='') as file:
# writer = csv.writer(file)
# writer.writerow(origin_headers)
recruitInfosByGroup = manager.list()
with closing(Pool(processes=5)) as pool:
pool.starmap(getRecruitInfoList, zip(groups, repeat(recruitInfosByGroup)))
insertDB("recruitInfos", recruitInfosByGroup)
print('μ§κ΅°λ³ μ±μ©κ³΅κ³ 리μ€νΈ url μ μ₯ μλ£!')
return recruitInfosByGroup
def getAllElement(driver, recruitInfoUrl):
whereElement, tagElements, companyElement, detailElements, whereElement, deadlineElement, workAreaElement \
= '', '', '', '', '', '', ''
try:
companyElement = driver.find_element_by_xpath('//section[@class="Bfoa2bzuGpxK9ieE1GxhW"]/div/h6/a')
except Exception:
saveError("elementError", recruitInfoUrl, 'warning: companyElement is null')
try:
detailElements = driver.find_elements_by_xpath('//section[@class="_1LnfhLPc7hiSZaaXxRv11H"]/p')
except Exception:
saveError("elementError", recruitInfoUrl, 'warning: detailElements is null')
try:
tagElements = driver.find_elements_by_xpath('//div[@class="ObubI7m2AFE5fxlR8Va9t"]/ul/li/a')
except Exception:
saveError("elementError", recruitInfoUrl, 'warning: tagElements is null')
try:
whereElement = driver.find_element_by_xpath(
'/html/body/div[1]/div/div[3]/div[1]/div[1]/div/section[2]/div[1]/span')
except Exception:
saveError("elementError", recruitInfoUrl, 'warning: whereElement is null')
try:
workAreaElement = driver.find_element_by_xpath(
'/html/body/div[1]/div/div[3]/div[1]/div[1]/div/div[2]/section[2]/div[2]/span[2]')
except Exception:
saveError("elementError", recruitInfoUrl, 'warning: workAreaElement is null')
try:
deadlineElement = driver.find_element_by_xpath(
'/html/body/div[1]/div/div[3]/div[1]/div[1]/div[1]/div[2]/section[2]/div[1]/span[2]')
except Exception:
saveError("elementError", recruitInfoUrl, 'warning: deadlineElement is null')
return [whereElement, tagElements, companyElement, detailElements, workAreaElement, deadlineElement]
def getInfosByElements(elements):
tagPattern = '[^0-9a-zA-Zγ±-ν%:.~\n]'
detailPattern = '[^0-9a-zA-Zγ±-ν%:.~ #+\n]'
region, _ = elements[0].text.split('\n.\n') if elements[0] else [None, None]
tags = [re.sub(pattern=tagPattern, repl='', string=tagElement.text) for tagElement in elements[1]] if elements[
1] else []
company = elements[2].text if elements[2] else None
workArea = elements[4].text if elements[4] else None
deadline = elements[5].text if elements[5] else None
details, detailsNouns = [], []
if not elements[3]: details = []
for detailElement in elements[3]:
detail = re.sub(pattern=detailPattern, repl='', string=detailElement.text).strip()
details.append(detail)
englishTokens = nltk.word_tokenize(re.sub(f'[^a-zA-Z]', ' ', detailElement.text).strip())
english = [token.lower() for token in englishTokens if token not in stopwordsEN]
postprocessor = Postprocessor(twitter, passtags='Noun', ngrams=ngrams, stopwords=stopwordsKR)
koreanWords = postprocessor.pos(detail)
# koreanWords = okt.nouns(detail)
print(koreanWords)
korean = [word for word, _ in koreanWords if len(word) > 1 and word != 'μ±']
others = re.findall('[\d{2}]λ
]', detailElement.text)
temp = []
temp.extend(korean)
temp.extend(english)
temp.extend(others)
detailsNouns.append(temp)
print(detailsNouns)
return region, tags, company, details, deadline, workArea, detailsNouns
def createrecruitInfo(contents):
# headers = ['id', 'μ§κ΅°', 'μ§μ', 'κ΅κ°', 'νκ·Έ', 'νμ¬λͺ
', 'νμ¬μκ°', 'μ£Όμμ
무', 'μ격μ건', 'μ°λμ¬ν', 'νν λ° λ³΅μ§', 'λ§κ°μΌ', '근무μ§']
headers = ['μ§κ΅°', 'μ§μ', 'νκ·Έ', 'νμ¬λͺ
', 'νμ¬μκ°', 'μ£Όμμ
무', 'μ격μ건', 'μ°λμ¬ν', 'νν λ° λ³΅μ§', 'λ§κ°μΌ', '근무μ§', 'νμ¬μκ°_λͺ
μ¬', 'μ£Όμμ
무_λͺ
μ¬', 'μ격μ건_λͺ
μ¬', 'μ°λμ¬ν_λͺ
μ¬', 'νν λ° λ³΅μ§_λͺ
μ¬']
recruitInfo = {header: value for header, value in zip(headers, contents)}
# with open('data/RecruitInfoLog.json', 'a', encoding='UTF-8') as file:
# json.dump(recruitInfo, file, indent=4, ensure_ascii=False)
# file.write('\n,')
return recruitInfo
def getRecruitInfo(recruitInfoUrl, allRecruitInfo):
print(recruitInfoUrl)
pattern = '[^0-9]'
group = recruitInfoUrl['jobGroup']
url = recruitInfoUrl['url']
# group = recruitInfoUrl[0]
# url = recruitInfoUrl[1].strip()
recruitInfoId = re.sub(pattern=pattern, repl='', string=url)
try:
driver = connectWebDriver(url)
except Exception as error:
saveError("connectionError", recruitInfoUrl, error.args)
return
recruitInfoElements = getAllElement(driver, recruitInfoUrl)
region, tags, company, details, deadline, workArea, detailsNouns = getInfosByElements(recruitInfoElements)
contents = []
contents.append(recruitInfoId)
contents.append(group)
contents.append(region)
contents.append(company)
contents.extend(details)
contents.append(deadline)
contents.append(workArea)
contents.extend(detailsNouns)
recruitInfo = createrecruitInfo(contents)
allRecruitInfo.append(recruitInfo)
insertDB("recruitInfo", recruitInfo)
# REQUIRED = [recruitInfoId, region, company, workArea]
# with open('data/recruitInfo/origin.csv', 'a', encoding='utf-8-sig', newline='') as file:
# writer = csv.writer(file)
# writer.writerow(contents)
# with open('data/recruitInfo/tag.csv', 'a', encoding='utf-8-sig', newline='') as file:
# writer = csv.writer(file)
# writer.writerow(REQUIRED+tags)
# # νμ¬μκ°
# with open('data/recruitInfo/introduction.csv', 'a', encoding='utf-8-sig', newline='') as file:
# writer = csv.writer(file)
# writer.writerow(REQUIRED+detailsNouns[0])
# # μ£Όμμ
무
# with open('data/recruitInfo/main_task.csv', 'a', encoding='utf-8-sig', newline='') as file:
# writer = csv.writer(file)
# writer.writerow(REQUIRED+detailsNouns[1])
# # μ격μ건
# with open('data/recruitInfo/requirement.csv', 'a', encoding='utf-8-sig', newline='') as file:
# writer = csv.writer(file)
# writer.writerow([recruitInfoId]+detailsNouns[2])
# # μ°λμ¬ν
# with open('data/recruitInfo/preference.csv', 'a', encoding='utf-8-sig', newline='') as file:
# writer = csv.writer(file)
# writer.writerow(REQUIRED+detailsNouns[3])
# # λ³΅μ§ λ° νν
# with open('data/recruitInfo/benefit.csv', 'a', encoding='utf-8-sig', newline='') as file:
# writer = csv.writer(file)
# writer.writerow(REQUIRED+detailsNouns[4])
print('μλ£!')
driver.quit()
def scrapRecruitInfo(recruitInfoURLs):
# origin_headers = ['id', 'μ§κ΅°', 'μ§μ', 'νμ¬λͺ
', 'νμ¬μκ°', 'μ£Όμμ
무', 'μ격μ건', 'μ°λμ¬ν', 'νν λ° λ³΅μ§', 'λ§κ°μΌ', '근무μ§']
# with open('data/recruitInfo/origin.csv', 'w', encoding='utf-8-sig', newline='') as file:
# writer = csv.writer(file)
# writer.writerow(origin_headers)
# with open('data/recruitInfo/tag.csv', 'w', encoding='utf-8-sig', newline='') as file:
# writer = csv.writer(file)
# writer.writerow(['id','νκ·Έ'])
# with open('data/recruitInfo/introduction.csv', 'w', encoding='utf-8-sig', newline='') as file:
# writer = csv.writer(file)
# writer.writerow(['id','νμ¬μκ°'])
# with open('data/recruitInfo/main_task.csv', 'w', encoding='utf-8-sig', newline='') as file:
# writer = csv.writer(file)
# writer.writerow(['id','μ£Όμμ
무'])
# with open('data/recruitInfo/requirement.csv', 'w', encoding='utf-8-sig', newline='') as file:
# writer = csv.writer(file)
# writer.writerow(['id','μ격μ건'])
# with open('data/recruitInfo/preference.csv', 'w', encoding='utf-8-sig', newline='') as file:
# writer = csv.writer(file)
# writer.writerow(['id','μ°λμ¬ν'])
# with open('data/recruitInfo/benefit.csv', 'w', encoding='utf-8-sig', newline='') as file:
# writer = csv.writer(file)
# writer.writerow(['id','νν λ° λ³΅μ§'])
allRecruitInfo = manager.list()
openJsonFile('data/logs/RecruitInfoError.json')
with closing(Pool(processes=5)) as pool:
pool.starmap(getRecruitInfo, zip(recruitInfoURLs, repeat(allRecruitInfo)))
print('μ±μ©μμΈμ 보 μμ§ λͺ¨λ μλ£!')
closeJsonFile('data/logs/RecruitInfoError.json')
if __name__ == '__main__':
manager = Manager()
# print('---------μ±μ©μ§κ΅°---------------------------')
# jobGroups = getJobGroups()
#
# print('---------μ±μ©κ³΅κ³ 리μ€νΈ----------------------')
# recruitInfosByGroup = scrapRecruitList(jobGroups)
#
# print('---------μ±μ©κ³΅κ³ ---------------------------')
# scrapRecruitInfo()
recruitInfosByGroup = readDB('recruitInfos')
# with open('data/recruitInfoList.csv', 'r', encoding='utf-8-sig', newline='') as file:
# recruitInfosByGroup = [line.split(',') for line in file]
# print(recruitInfosByGroup)
# recruitInfosByGroup = [['νλ‘ νΈμλ κ°λ°μ','https://www.wanted.co.kr/wd/42882']]
# recruitInfosByGroup =[{'jobGroup': 'νλ‘ νΈμλ κ°λ°μ', 'url': 'https://www.wanted.co.kr/wd/43046'}]
scrapRecruitInfo(recruitInfosByGroup)