Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,8 @@
|
|
|
|
|
|
1 |
from bs4 import BeautifulSoup
|
2 |
from itertools import combinations
|
3 |
-
from
|
4 |
-
import requests
|
5 |
|
6 |
# Paleta de colores
|
7 |
color_palette = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#bcbd22', '#17becf', '#ff6666', '#4dff4d', '#6666ff', '#ffcc00', '#993399', '#00cc99', '#ff5050', '#33adff', '#ff9966']
|
@@ -10,7 +11,7 @@ def buscar_google(query, hl='es', num_results=10):
|
|
10 |
all_results = []
|
11 |
|
12 |
for start in range(0, num_results, 10):
|
13 |
-
url = f"https://www.google.com/search?q={query}&hl={hl}&start={start}"
|
14 |
headers = {
|
15 |
"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"
|
16 |
}
|
@@ -29,59 +30,40 @@ def buscar_google(query, hl='es', num_results=10):
|
|
29 |
|
30 |
return serp_data
|
31 |
|
32 |
-
def
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
if serp1 and serp2 and serp1[0]["URL"] == serp2[0]["URL"]:
|
38 |
-
return min(100, round(len(common_urls) / len(urls_serp1) * 100 + 5))
|
39 |
-
elif serp1 and serp2 and serp1[1]["URL"] == serp2[1]["URL"]:
|
40 |
-
return min(100, round(len(common_urls) / len(urls_serp1) * 100 + 3))
|
41 |
-
else:
|
42 |
-
return min(100, round(len(common_urls) / len(urls_serp1) * 100))
|
43 |
-
|
44 |
-
def generar_html_con_colores(serp_results, color_palette):
|
45 |
-
html_table = "<table border='1'><tr><th>Posici贸n</th>"
|
46 |
|
47 |
for keyword in serp_results:
|
48 |
-
html_table += f"<th>{keyword}</th>"
|
49 |
html_table += "</tr>"
|
50 |
|
51 |
url_colors = {}
|
52 |
-
max_length = max(len(serp_results[keyword]) for keyword in
|
|
|
53 |
for i in range(max_length):
|
54 |
-
|
55 |
-
for keyword in
|
|
|
56 |
if i < len(serp_results[keyword]):
|
57 |
-
|
58 |
-
url
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
color = url_colors[url]
|
63 |
-
row_values.extend([f"<span style='color: {color}'>{url}</span>"])
|
64 |
-
else:
|
65 |
-
row_values.extend([url])
|
66 |
else:
|
67 |
-
|
68 |
-
html_table += "
|
69 |
html_table += "</table>"
|
70 |
return html_table
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
coincidencia_matrix = {}
|
81 |
-
for keyword1, keyword2 in combinations(keywords, 2):
|
82 |
-
coincidencia = calcular_coincidencias(serp_results[keyword1], serp_results[keyword2])
|
83 |
-
coincidencia_matrix[f"{keyword1} & {keyword2}"] = coincidencia
|
84 |
-
print(f"{keyword1} & {keyword2}: {coincidencia}%")
|
85 |
|
86 |
-
|
87 |
-
display(HTML(html_table_with_colors))
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
from bs4 import BeautifulSoup
|
4 |
from itertools import combinations
|
5 |
+
from urllib.parse import quote
|
|
|
6 |
|
7 |
# Paleta de colores
|
8 |
color_palette = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#bcbd22', '#17becf', '#ff6666', '#4dff4d', '#6666ff', '#ffcc00', '#993399', '#00cc99', '#ff5050', '#33adff', '#ff9966']
|
|
|
11 |
all_results = []
|
12 |
|
13 |
for start in range(0, num_results, 10):
|
14 |
+
url = f"https://www.google.com/search?q={quote(query)}&hl={hl}&start={start}"
|
15 |
headers = {
|
16 |
"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"
|
17 |
}
|
|
|
30 |
|
31 |
return serp_data
|
32 |
|
33 |
+
def generar_html_con_colores(keywords):
|
34 |
+
serp_results = {keyword: buscar_google(keyword) for keyword in keywords.split(',')}
|
35 |
+
html_table = "<style>td, th {border: 1px solid #ddd; text-align: left; padding: 8px;} tr:nth-child(even) {background-color: #f2f2f2;} th {padding-top: 11px; padding-bottom: 11px; background-color: #4CAF50; color: white;}</style>"
|
36 |
+
html_table += "<table><tr><th>Posici贸n</th>"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
for keyword in serp_results:
|
39 |
+
html_table += f"<th>{keyword.strip()}</th>"
|
40 |
html_table += "</tr>"
|
41 |
|
42 |
url_colors = {}
|
43 |
+
max_length = max(len(serp_results[keyword.strip()]) for keyword in keywords.split(','))
|
44 |
+
|
45 |
for i in range(max_length):
|
46 |
+
html_table += "<tr><td>{}</td>".format(i + 1)
|
47 |
+
for keyword in keywords.split(','):
|
48 |
+
keyword = keyword.strip()
|
49 |
if i < len(serp_results[keyword]):
|
50 |
+
url = serp_results[keyword][i]["URL"]
|
51 |
+
if url not in url_colors:
|
52 |
+
url_colors[url] = color_palette[len(url_colors) % len(color_palette)]
|
53 |
+
color = url_colors[url]
|
54 |
+
html_table += "<td><span style='color:{}'>{}</span></td>".format(color, serp_results[keyword][i]["T铆tulo"])
|
|
|
|
|
|
|
|
|
55 |
else:
|
56 |
+
html_table += "<td></td>"
|
57 |
+
html_table += "</tr>"
|
58 |
html_table += "</table>"
|
59 |
return html_table
|
60 |
|
61 |
+
iface = gr.Interface(
|
62 |
+
fn=generar_html_con_colores,
|
63 |
+
inputs="text",
|
64 |
+
outputs="html",
|
65 |
+
title="Comparador de Keywords en Google",
|
66 |
+
description="Introduce las keywords separadas por comas para encontrar coincidencias en los resultados de b煤squeda de Google."
|
67 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
+
iface.launch()
|
|