Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
243 lines (164 loc) · 5.1 KB
/
Copy pathmain.py
File metadata and controls
243 lines (164 loc) · 5.1 KB
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
# ============================================================
# HTML SCANNER
# Digunakan untuk memeriksa struktur HTML sebuah website
# ============================================================
import requests
import sys
from bs4 import BeautifulSoup
from collections import Counter
URL = input("Masukkan URL website: ")
print("\nMengakses website...")
print("URL:", URL)
# Mengambil HTML dari website
try:
response = requests.get(
URL,
headers={"User-Agent": "Mozilla/5.0"},
timeout=10
)
print("Status code:", response.status_code)
response.raise_for_status()
except requests.RequestException as error:
print("Gagal mengakses website.")
print("Error:", error)
sys.exit()
soup = BeautifulSoup(response.text, "html.parser")
print("HTML berhasil dibaca.")
# Menghitung semua tag HTML
tags = [tag.name for tag in soup.find_all()]
tag_counts = Counter(tags)
print("\n" + "=" * 60)
print("DAFTAR TAG HTML")
print("=" * 60)
for tag_name, jumlah in tag_counts.most_common():
print(f"{tag_name:<20} {jumlah}")
# Menghitung semua class HTML
class_counts = Counter()
for tag in soup.find_all():
classes = tag.get("class", [])
for class_name in classes:
class_counts[class_name] += 1
print("\n" + "=" * 60)
print("DAFTAR CLASS HTML")
print("=" * 60)
if class_counts:
for class_name, jumlah in class_counts.most_common():
print(f"{class_name:<30} {jumlah}")
else:
print("Tidak ditemukan class.")
# Menghitung semua ID HTML
id_counts = Counter()
for tag in soup.find_all():
tag_id = tag.get("id")
if tag_id:
id_counts[tag_id] += 1
print("\n" + "=" * 60)
print("DAFTAR ID HTML")
print("=" * 60)
if id_counts:
for id_name, jumlah in id_counts.most_common():
print(f"{id_name:<30} {jumlah}")
else:
print("Tidak ditemukan ID.")
# Menghitung penggunaan atribut HTML
attribute_counts = Counter()
for tag in soup.find_all():
for attribute_name in tag.attrs:
attribute_counts[attribute_name] += 1
print("\n" + "=" * 60)
print("DAFTAR ATRIBUT HTML")
print("=" * 60)
for attribute_name, jumlah in attribute_counts.most_common():
print(f"{attribute_name:<30} {jumlah}")
# Menampilkan link HTML
links = soup.find_all("a")
print("\n" + "=" * 60)
print("LINK / <a>")
print("=" * 60)
print("Jumlah link:", len(links))
for nomor, link in enumerate(links[:20], start=1):
text_link = link.get_text(" ", strip=True)
alamat_link = link.get("href")
print(f"\n{nomor}. Text : {text_link}")
print(f" Href : {alamat_link}")
# Menampilkan gambar HTML
images = soup.find_all("img")
print("\n" + "=" * 60)
print("GAMBAR / <img>")
print("=" * 60)
print("Jumlah gambar:", len(images))
for nomor, image in enumerate(images[:20], start=1):
sumber_gambar = image.get("src")
teks_alt = image.get("alt")
print(f"\n{nomor}. SRC : {sumber_gambar}")
print(f" ALT : {teks_alt}")
# Menampilkan informasi tabel HTML
tables = soup.find_all("table")
print("\n" + "=" * 60)
print("TABEL / <table>")
print("=" * 60)
print("Jumlah tabel:", len(tables))
for nomor, table in enumerate(tables, start=1):
table_class = table.get("class")
table_id = table.get("id")
jumlah_baris = len(table.find_all("tr"))
print(f"\nTabel {nomor}")
print("Class :", table_class)
print("ID :", table_id)
print("Baris :", jumlah_baris)
# Menampilkan informasi form HTML
forms = soup.find_all("form")
print("\n" + "=" * 60)
print("FORM / <form>")
print("=" * 60)
print("Jumlah form:", len(forms))
for nomor, form in enumerate(forms, start=1):
action = form.get("action")
method = form.get("method")
print(f"\nForm {nomor}")
print("Action :", action)
print("Method :", method)
# Menampilkan struktur HTML secara bertingkat
def tampilkan_struktur(element, level=0, batas_level=3):
if element is None or level > batas_level:
return
if element.name is None:
return
indentasi = " " * level
informasi = element.name
classes = element.get("class")
tag_id = element.get("id")
if classes:
informasi += f" class={classes}"
if tag_id:
informasi += f" id={tag_id}"
print(indentasi + informasi)
for child in element.find_all(recursive=False):
tampilkan_struktur(
child,
level + 1,
batas_level
)
print("\n" + "=" * 60)
print("CONTOH STRUKTUR HTML")
print("=" * 60)
tampilkan_struktur(soup.html)
# Menampilkan ringkasan hasil scanning
print("\n" + "=" * 60)
print("RINGKASAN HTML")
print("=" * 60)
print("URL :", URL)
print("Status code :", response.status_code)
print("Ukuran HTML :", len(response.text), "karakter")
print("Jumlah tag :", len(tags))
print("Jenis tag :", len(tag_counts))
print("Jumlah class :", len(class_counts))
print("Jumlah ID :", len(id_counts))
print("Jenis atribut :", len(attribute_counts))
print("Jumlah link :", len(links))
print("Jumlah gambar :", len(images))
print("Jumlah tabel :", len(tables))
print("Jumlah form :", len(forms))
print("=" * 60)
print("Scanning selesai.")
print("=" * 60)