Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
53 lines (42 loc) · 1.68 KB
/
Copy pathmain.py
File metadata and controls
53 lines (42 loc) · 1.68 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
import requests
from bs4 import BeautifulSoup
import re
import csv
def load_urls(file_path):
with open(file_path, "r") as file:
return [line.strip() for line in file.readlines()]
def find_google_analytics_tags(url):
try:
response = requests.get(url, timeout=5)
soup = BeautifulSoup(response.text, "html.parser")
scripts = soup.find_all("script")
analytics_tags = []
patterns = {
"GA4": re.compile(r'G-[A-Z0-9]+'),
"GTM": re.compile(r'GTM-[A-Z0-9]+'),
"UA": re.compile(r'UA-[0-9]+-[0-9]+')
}
for script in scripts:
script_text = script.text
for tag_type, pattern in patterns.items():
matches = pattern.findall(script_text)
if matches:
analytics_tags.append((tag_type, matches[0]))
return analytics_tags if analytics_tags else "Nenhuma tag encontrada"
except Exception as e:
return f"Erro: {str(e)}"
def save_results(results, file_path="output/analytics_results.csv"):
with open(file_path, "w", newline="") as file:
writer = csv.writer(file)
writer.writerow(["URL", "Tag Tipo", "Tag Encontrada"])
for url, tags in results.items():
if isinstance(tags, list):
for tag_type, tag_value in tags:
writer.writerow([url, tag_type, tag_value])
else:
writer.writerow([url, "Erro", tags])
if __name__ == "__main__":
urls = load_urls("urls.txt")
results = {url: find_google_analytics_tags(url) for url in urls}
save_results(results)
print("Análise concluída! em'output/analytics_results.csv'")