-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle_page.py
171 lines (146 loc) · 4.97 KB
/
handle_page.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
import requests
from bs4 import BeautifulSoup
import sys
from urllib.parse import urlsplit
from PIL import Image
import json
import os
import unicodedata
import re
import io
class HandlePage:
def __init__(self, url):
self.url = url
self.path_artigos = "./artigos"
self.dict = {}
self.titulo = None
self.soup = None
self.jsonobj = None
self.text = None
self.autor = None
self.data = None
self.image_url = None
def getSoup(self):
response = requests.get(self.url)
soup = BeautifulSoup(response.content, 'html.parser')
self.soup = soup
def cleanText(self, text):
text = text.replace('\n', ' ')
cleanText = []
# Parametro criado para auxiliar a remover referencias do texto #
ref = False
for ch in text:
if ch == "[":
ref = True
elif ch == "]":
ref = False
elif ref:
pass
else:
nfkd = unicodedata.normalize('NFKD', ch)
filter = re.sub('[^A-z 0-9 , . ( ) / - -\\\]', '', nfkd)
char = u"".join([c for c in filter if not unicodedata.combining(c)])
cleanText.append(char)
return ''.join(cleanText)
def getTitulo(self):
titulo = self.soup.title.string.split("–")[0].rstrip().lower()
self.titulo = self.cleanText(titulo)
def getTexto(self):
bodydiv = self.soup.body.find("div",{"itemprop":"articleBody"})
bodycontent = bodydiv.find_all(lambda tag: tag.name == 'p' and not tag.attrs)
bodycontentfinal = ""
for p in bodycontent[1:]:
bodycontentfinal += p.get_text() + " "
self.text = self.cleanText(bodycontentfinal)
def getAutor(self):
try:
bodydiv = self.soup.body.find("div",{"itemprop":"articleBody"})
autor, data = bodydiv.p.getText().split('\n')
except Exception as e:
autor = "Vazio"
pass
self.autor = autor
def getData(self):
try:
bodydiv = self.soup.body.find("div",{"itemprop":"articleBody"})
autor, data = bodydiv.p.getText().split('\n')
except Exception as e:
data = "Vazio"
pass
self.data = data
def getImageUrl(self):
image_url = self.soup.find('figure').img['src']
self.image_url = image_url
def getJson(self):
dict = {
"url": self.url,
"titulo": self.titulo,
"autor": self.autor,
"data": self.data,
"imgurl": self.image_url
}
self.dict = dict #jsonobj
def saveJson(self):
nomearquivo = self.titulo+".json"
nomecompleto = os.path.join(self.path_artigos, nomearquivo)
f = open(nomecompleto, "w", encoding = "ascii")
json.dump(self.dict, f, indent = 2)
f.close()
#return True
def saveConteudo(self):
nomearquivo = self.titulo+".txt"
nomecompleto = os.path.join(self.path_artigos, nomearquivo)
f = open(nomecompleto, "w")
f.write(self.text)
f.close()
#return True
def saveImg(self):
parts = urlsplit(self.image_url)
paths = parts.path.split('/')
formatoimg = "." + paths[::-1][0].split('.')[::-1][0]
if formatoimg == ".php" or formatoimg == ".article": formatoimg = ".png"
try:
img = Image.open(requests.get(self.image_url, stream = True).raw).convert("RGB")
except:
img = Image.open(io.BytesIO(requests.get(self.image_url, stream=True).content)).convert("RGB")
formatoimg = ".png"
nomearquivoimg = self.titulo + formatoimg
nomecompleto = os.path.join(self.path_artigos, nomearquivoimg)
img.save(nomecompleto)
def main():
# Funcao principal da aplicacao #
if sys.argv[1] == "--help" or sys.argv[1] == "-h":
print("""
Handle Page ( https://github.com/0xdferraz/Saense-PLN )
Extrai o texto, imagem e informacoes de uma postagem do Saense ( https://saense.com.br/ )
Uso: python handle_page.py <URL do Artigo>
""")
exit()
else:
url = sys.argv[1]
page = HandlePage(url)
# Executa os metodos necessarios para adquirir os dados #
page.getSoup()
page.getTitulo()
print(page.titulo)
page.getTexto()
page.getAutor()
page.getData()
page.getImageUrl()
page.getJson()
# Salva as informacoes #
try:
page.saveConteudo()
except Exception as e:
print("\nErro","'" + e + "'","ao salvar Texto", page.titulo)
try:
page.saveJson()
except Exception as e:
print("\nErro","'" + e + "'","ao salvar Json", page.titulo)
try:
page.saveImg()
except Exception as e:
print("\nErro","'" + e + "'","ao salvar Imagem", page.titulo)
print("FIM.")
if __name__ == "__main__":
main()