-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
169 lines (135 loc) · 4.96 KB
/
run.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
# -*- coding: utf-8 -*-
#######################################
## Empregos OLX SP
## Busca de empregos no olx com url, titulo, anunciante, contato, descricao, local, data
##
## Author: Alex Benincasa Santos
## Mail: [email protected]
## 2019
#######################################
import os
import re
import time
import json
import hashlib
import requests
from datetime import datetime as date
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By as by
from selenium.webdriver.support.ui import WebDriverWait
from concurrent.futures import ThreadPoolExecutor,as_completed
from bs4 import BeautifulSoup
# caminho para o binário do geckodriver.exe
bin_path = 'C:\\www\\olx-empregos\\geckodriver.exe'
# função para pegar os dados do emprego
def salvarVaga(url):
try:
options = Options()
options.headless = True
browser = webdriver.Firefox(executable_path=bin_path, options=options)
wait = WebDriverWait(browser, 15)
browser.get(url)
titulo = browser.find_element_by_css_selector('h1').text.replace('/','')
# print(f'[{titulo}] titulo')
contato = []
# print(f'[{titulo}] contato')
for div in browser.find_elements_by_css_selector('#content div > div div'):
if 'ver número' in div.text:
# clicar em todos os números da descrição
for a in div.find_elements_by_css_selector('a'):
if '9' in a.text:
a.click()
# print(f'[{titulo}] svg in a.innerHTML')
# print(f'[{titulo}] span in p')
for span in div.find_elements_by_css_selector('p'):
if '9' in span.text:
# regex de números brasileiros
regex_result = re.findall(r'((\([0-9]{2}(\) )?|[0-9]{2}( )?|([0-9]{2})?)?[0-9]{5}( |-)?[0-9]{4})',span.text.replace('.',''))
for tel in regex_result:
if len(tel[0])>4:
contato.append(tel[0])
break
break
for a in browser.find_elements_by_css_selector('#content a'):
#print(a.get_attribute('innerHTML'))
if 'svg' in a.get_attribute('innerHTML'):
a.click()
mail = re.findall(r'[\w\.-]+@[\w\.-]+', div.text)
if mail:
contato.append(mail[0])
descricao = div.text
for div in browser.find_elements_by_css_selector('#content div > div > div div'):
if 'Publicado em' in div.text:
ano = date.now().year
data_published = div.text.replace('Publicado em ','').replace(' às ',f'/{ano} ').split('\n')[0]
data_published = date.strptime(data_published, '%d/%m/%Y %H:%M').strftime('%Y-%m-%dT%H:%M:00')
break
# print(f'[{titulo}] descricao = div.text')
if len(contato)==0:
browser.quit()
raise ValueError('Vaga não tem contato')
anunciante = browser.find_element_by_css_selector('#miniprofile span').text
# print(f'[{titulo}] anunciante')
vaga = {}
vaga['fonte'] = 'OLX Empregos'
vaga['url'] = url.strip()
vaga['titulo'] = titulo.strip()
vaga['anunciante'] = anunciante.strip()
vaga['contato'] = contato
vaga['descricao'] = descricao.strip()
vaga['local'] = ''
vaga['data'] = data_published
local = None
has_municipio = False
has_bairro = False
for info in browser.find_elements_by_css_selector('dt,dd'):
if 'Município' in info.text:
has_municipio = True
continue
if 'Bairro' in info.text:
has_bairro = True
continue
if has_municipio:
vaga['local'] = info.text.strip()
has_municipio = False
local = vaga['local']
if has_bairro:
bairro = info.text.strip()
if local:
vaga['local'] += ' - '+bairro
elif bairro:
vaga['local'] = bairro
has_bairro = False
anuncio = vaga['titulo']+vaga['anunciante']+vaga['local']
browser.quit()
# o hash vai identificar a vaga
hash = 'data/'+hashlib.sha224(anuncio.strip().encode('utf-8')).hexdigest()
fname = hash+(f'-{titulo}.json'.lower().replace('/',''))
if os.path.isfile(fname):
raise ValueError('Vaga já foi cadastrada')
# salvar o arquivo com os dados da vaga
with open(fname, mode="w") as f:
f.write(json.dumps(vaga, indent=4))
except Exception as e:
print(f'[{titulo}] '+str(e))
browser.quit()
exit()
links = []
for page in range(1,20):
# sp.olx.com.br Busca em São Paulo
# o={} Paginação
result = requests.get('https://sp.olx.com.br/vagas-de-emprego?o={}'.format(page))
# BeautifulSoup faz o parser no result colocando as tags
soup = BeautifulSoup(result.text, features='html.parser')
for li in soup.select('ul.list li.item a.OLXad-list-link-featured'):
links.append(li['href'])
# max_workers=3 pra não travar seu pc
# threading no python para executar mais rápido (de forma assíncrona)
with ThreadPoolExecutor(max_workers=3) as executor:
for thread in as_completed({ executor.submit(salvarVaga,url): url for url in links }):
try:
thread.result()
except Exception as e:
print(e)