Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,44 +6,78 @@ from urllib.parse import urlparse
|
|
6 |
from itertools import combinations
|
7 |
|
8 |
def buscar_google(query, dominio, hl='es', num_results=100):
|
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 |
html_output += "<details style='padding: 10px; background: #f3f3f3; border: solid 0; border-radius: 8px; margin-top: 10px;'>"
|
46 |
-
html_output +=
|
47 |
html_output += "</details>"
|
48 |
|
49 |
return html_output
|
|
|
6 |
from itertools import combinations
|
7 |
|
8 |
def buscar_google(query, dominio, hl='es', num_results=100):
|
9 |
+
all_results = []
|
10 |
+
posiciones_dominio = []
|
11 |
+
posiciones_dominio_exacto = []
|
12 |
+
url_objetivo = dominio
|
13 |
+
dominio_objetivo = urlparse(url_objetivo).netloc
|
14 |
+
|
15 |
+
table_html = "<summary>Ver Resultados</summary><table border='1'><tr><th>Posici贸n</th><th>T铆tulo</th><th>URL</th></tr>"
|
16 |
+
|
17 |
+
estilo = "color: #ed4b4b;"
|
18 |
+
|
19 |
+
for start in range(0, num_results, 10):
|
20 |
+
url = f"https://www.google.com/search?q={query}&hl={hl}&start={start}"
|
21 |
+
headers = {
|
22 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.3"
|
23 |
+
}
|
24 |
+
response = requests.get(url, headers=headers)
|
25 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
26 |
+
search_results = soup.find_all('div', attrs={'class': 'tF2Cxc'})
|
27 |
+
all_results.extend(search_results)
|
28 |
+
|
29 |
+
for i, result in enumerate(all_results[:num_results]):
|
30 |
+
header = result.find('h3')
|
31 |
+
header = header.text if header else "Sin t铆tulo"
|
32 |
+
link = result.find('a', href=True)['href']
|
33 |
+
link_clean = re.search("(?P<url>https?://[^\s]+)", link).group("url")
|
34 |
+
dominio_resultado = urlparse(link_clean).netloc
|
35 |
+
|
36 |
+
estilo_dominio = ""
|
37 |
+
if dominio_objetivo in dominio_resultado:
|
38 |
+
posiciones_dominio.append(i + 1)
|
39 |
+
if link_clean == url_objetivo:
|
40 |
+
posiciones_dominio_exacto.append(i + 1)
|
41 |
+
estilo_dominio = estilo
|
42 |
+
else:
|
43 |
+
estilo_dominio = estilo
|
44 |
+
|
45 |
+
table_html += f"<tr><td>{i+1}</td><td>{header}</td><td><span><a href='{link_clean}' title='{link_clean}' style='{estilo_dominio}'>{link_clean}</a></span></td></tr>"
|
46 |
+
|
47 |
+
table_html += "</table>"
|
48 |
+
|
49 |
+
mensaje = ""
|
50 |
+
emoji = ""
|
51 |
+
if posiciones_dominio:
|
52 |
+
first_position = posiciones_dominio[0]
|
53 |
+
if first_position == 1:
|
54 |
+
emoji = f"{posiciones_dominio} 馃"
|
55 |
+
elif first_position <= 3:
|
56 |
+
emoji = f"{posiciones_dominio} 馃槑"
|
57 |
+
elif first_position <= 5:
|
58 |
+
emoji = f"{posiciones_dominio} 馃槅"
|
59 |
+
elif first_position <= 10:
|
60 |
+
emoji = f"{posiciones_dominio} 馃檪"
|
61 |
+
elif first_position <= 100:
|
62 |
+
emoji = f"{posiciones_dominio} 馃槓"
|
63 |
+
else:
|
64 |
+
emoji = f"No est谩 en el top 100 馃槶"
|
65 |
+
|
66 |
+
if len(posiciones_dominio) > 1 and 1 in posiciones_dominio:
|
67 |
+
mensaje = f"Parasitaci贸n SEO: {url_objetivo} se encuentra en las posiciones {posiciones_dominio}"
|
68 |
+
elif len(posiciones_dominio) > 1:
|
69 |
+
mensaje = f"Canibalizaci贸n: {url_objetivo} se encuentra en las posiciones {posiciones_dominio}"
|
70 |
+
elif len(posiciones_dominio) == 1 and len(posiciones_dominio_exacto) == 0:
|
71 |
+
mensaje = f"Canibalizaci贸n: URL diferente del dominio en la posici贸n {posiciones_dominio[0]}"
|
72 |
+
elif len(posiciones_dominio) == 1:
|
73 |
+
mensaje = f"Sin canibalizaci贸n: {url_objetivo} se encuentra en la posici贸n {posiciones_dominio[0]}"
|
74 |
+
else:
|
75 |
+
mensaje = f"{url_objetivo} no se encuentra en el top 100"
|
76 |
+
|
77 |
+
mensaje_completo = f"Posici贸n: {emoji}<br>{mensaje}"
|
78 |
+
html_output = f"<div><b>{mensaje_completo}</b></div>"
|
79 |
html_output += "<details style='padding: 10px; background: #f3f3f3; border: solid 0; border-radius: 8px; margin-top: 10px;'>"
|
80 |
+
html_output += table_html
|
81 |
html_output += "</details>"
|
82 |
|
83 |
return html_output
|