-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
168 lines (119 loc) · 4.41 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
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
import json
import os
import requests
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
ENDPOINT = 'https://biblias.com.br/acfonline-versos'
def get_bible_html():
"""Busca e salva os capítulos em html na pasta acf"""
for book in range(1, 67):
chapter = 1
while True:
if book <= 27:
testament = 'nt'
elif book > 27:
testament = 'at'
path = os.path.join(
os.getcwd(),
'acf-html',
f'{testament}_book-{book}_chapter-{chapter}.html',
)
if os.path.exists(path):
print(f'Livro: {book}, capitulo: {chapter} JÁ EXISTE!')
chapter += 1
continue
url = ENDPOINT.format(book, chapter)
params = {'livro': book, 'capitulo': chapter}
req = requests.get(url, params=params, timeout=None)
if len(req.text) == 0:
break
with open(path, 'w') as f:
f.write(req.text)
print(f'Livro: {book}, capitulo: {chapter} SALVO!')
chapter += 1
def get_list_books_json():
"""Busca a lista de livros e salva em json"""
books = {}
driver = webdriver.Firefox()
driver.get('https://biblias.com.br/acfonline')
for book_num in range(1, 67):
if book_num <= 27:
driver.find_element(By.ID, 'btn-novo-testamento').click()
elif book_num > 27:
driver.find_element(By.ID, 'btn-antigo-testamento').click()
elem = driver.find_element(By.ID, f'livro-{book_num}')
books[book_num] = elem.text
driver.close()
path = os.path.join(
os.getcwd(),
'acf-json',
'_books.json',
)
save_json(books, path, 4)
def get_bible_json():
"""Busca e salva os capítulos em json na pasta acf-json"""
driver = webdriver.Firefox()
driver.get('https://biblias.com.br/acfonline')
for book in range(1, 67):
chapter = 1
while True:
book_data = {}
if book <= 27:
testament = 'nt'
btn = driver.find_element(By.ID, 'btn-novo-testamento')
driver.execute_script('arguments[0].click();', btn)
elif book > 27:
testament = 'at'
btn = driver.find_element(By.ID, 'btn-antigo-testamento')
driver.execute_script('arguments[0].click();', btn)
path = os.path.join(
os.getcwd(),
'acf-json',
f'{testament}_book-{book}_chapter-{chapter}.json',
)
if os.path.exists(path):
print(f'(JSON) Livro: {book}, capitulo: {chapter} JÁ EXISTE!')
chapter += 1
continue
book_btn_el = driver.find_element(By.ID, f'livro-{book}')
book_data['book'] = book
book_data['book_name'] = book_btn_el.text
book_data['chapter'] = chapter
book_btn_el.click()
try:
driver.find_element(
By.ID, f'capitulo-{book}-{chapter}'
).click()
except NoSuchElementException:
break
book_text = driver.find_element(By.ID, 'livro-texto')
while True:
try:
book_text.find_element(By.ID, 'loader')
continue
except NoSuchElementException:
break
book_verses = book_text.find_elements(By.CLASS_NAME, 'verse-text')
book_data['verses'] = []
for index, verse_el in enumerate(book_verses):
verse = {str(index + 1): verse_el.get_attribute('innerHTML')}
book_data['verses'].append(verse)
if len(book_data['verses']) == 0:
continue
save_json(book_data, path)
chapter += 1
driver.close()
def save_json(data, path, indent=None):
"""Salva um dict como json
Args:
data (dict): Dados
path (str): Path do arquivo
indent (int, optional): Valor de identação do json. Defaults to None.
"""
with open(path, 'w') as f:
f.write(json.dumps(data, indent=indent))
if __name__ == '__main__':
get_bible_html()
get_list_books_json()
get_bible_json()