informatica:programacion:python:modulos:beautiful_soup
Diferencias
Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anteriorRevisión previaPróxima revisión | Revisión previa | ||
| informatica:programacion:python:modulos:beautiful_soup [2024/06/14 16:14] – [Recursos] tempwin | informatica:programacion:python:modulos:beautiful_soup [2024/06/14 17:24] (actual) – [Recursos] tempwin | ||
|---|---|---|---|
| Línea 132: | Línea 132: | ||
| """ | """ | ||
| Buscar por un texto (y aprovechar el resultado para obtener el elemento padre) | Buscar por un texto (y aprovechar el resultado para obtener el elemento padre) | ||
| + | Queremos llegar a un precio que comienza por el signo " | ||
| """ | """ | ||
| Línea 140: | Línea 141: | ||
| </ | </ | ||
| + | ===== Búsquedas ===== | ||
| + | |||
| + | * '' | ||
| + | * '' | ||
| + | |||
| + | Podemos buscar más de un elemento a la vez: | ||
| + | |||
| + | <code python> | ||
| + | doc.find_all([" | ||
| + | </ | ||
| + | |||
| + | Podemos buscar un elemento que tenga cierto texto: | ||
| + | |||
| + | <code python> | ||
| + | doc.find_all([" | ||
| + | </ | ||
| + | |||
| + | Buscar atributos de cierto elemento, por ejemplo el atributo '' | ||
| + | |||
| + | <code python> | ||
| + | doc.find_all([" | ||
| + | </ | ||
| + | |||
| + | Búsqueda por clases CSS: | ||
| + | |||
| + | <code python> | ||
| + | tag = doc.find_all(class_=" | ||
| + | </ | ||
| + | |||
| + | <WRAP center round info 60%> | ||
| + | '' | ||
| + | </ | ||
| + | |||
| + | Búsqueda por expresión regular: | ||
| + | |||
| + | <code python> | ||
| + | import re | ||
| + | |||
| + | # Buscar un signo de dólar y lo que venga después | ||
| + | tags = doc.find_all(text=re.compile(" | ||
| + | </ | ||
| + | |||
| + | Limitar los resultados de búsqueda: | ||
| + | |||
| + | <code python> | ||
| + | import re | ||
| + | |||
| + | # Buscar un signo de dólar y lo que venga después | ||
| + | tags = doc.find_all(text=re.compile(" | ||
| + | </ | ||
| + | |||
| + | Búsqueda por proximidad en la estructura de árbol, por ejemplo, nodos hermanos, padre y descendientes: | ||
| + | |||
| + | <code python> | ||
| + | from bs4 import BeautifulSoup | ||
| + | import requests | ||
| + | |||
| + | url = " | ||
| + | result = requests.get(url).text | ||
| + | doc = BeautifulSoup(result, | ||
| + | |||
| + | tbody = doc.tbody | ||
| + | trs = tbody.contents | ||
| + | |||
| + | print(trs[1].next_sibling) | ||
| + | |||
| + | # Nodo padre: | ||
| + | print(trs[0].parent) | ||
| + | |||
| + | # Descendientes | ||
| + | print(trs[0].descendants) | ||
| + | # | ||
| + | # | ||
| + | </ | ||
| + | |||
| + | Recorrer una tabla buscando precios: | ||
| + | |||
| + | <code python> | ||
| + | from bs4 import BeautifulSoup | ||
| + | import requests | ||
| + | |||
| + | url = " | ||
| + | result = requests.get(url).text | ||
| + | doc = BeautifulSoup(result, | ||
| + | |||
| + | tbody = doc.tbody | ||
| + | trs = tbody.contents | ||
| + | |||
| + | prices = {} | ||
| + | |||
| + | for tr in trs[:10]: | ||
| + | for td in tr.contents: | ||
| + | name, price = tr.contents[2: | ||
| + | fixed_name = name.p.string | ||
| + | fixed_price = price.a.string | ||
| + | | ||
| + | prices[fixed_name] = fixed_price | ||
| + | | ||
| + | print(prices) | ||
| + | </ | ||
| ===== Recursos ===== | ===== Recursos ===== | ||
| * [[https:// | * [[https:// | ||
| + | * [[https:// | ||
informatica/programacion/python/modulos/beautiful_soup.1718374499.txt.gz · Última modificación: por tempwin
