forked from mouredev/retos-programacion-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
Retos/Reto #18 - WEB SCRAPING [Difícil]/python/vieitesss.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import re | ||
import requests | ||
from bs4 import BeautifulSoup | ||
|
||
URL = "https://holamundo.day" | ||
# Cadena de texto principal, desde dónde queremos empezar a obtener los datos | ||
TARGET = "Agenda 8 de mayo" | ||
|
||
# Obtiene los datos de la página | ||
page = requests.get(URL) | ||
|
||
# Busca dónde aparece el text objetivo | ||
coincidence = re.search(f".*{TARGET}", page.text) | ||
|
||
if coincidence: | ||
end = coincidence.end() | ||
# Si encuentra el texto, se queda únicamente con el contenido que se encuentra | ||
# justo después | ||
new_text = page.text[end:] | ||
|
||
# Parsea el nuevo texto | ||
soup = BeautifulSoup(new_text, "html.parser") | ||
|
||
# Busca todos los tags "blockquote", los cuales incluyen los "span" con el texto | ||
# que se busca | ||
blockquotes = soup.findAll("blockquote") | ||
|
||
for block in blockquotes: | ||
text = "" | ||
# Dentro de cada "blockquote", por cada uno de los "span" con el atributo | ||
# "data-slate-string" = "true", obtiene su texto y lo va encadenando | ||
for span in block.findAll("span", {"data-slate-string": "true"}): | ||
text = f"{text}{span.text}" | ||
|
||
print(text) |