Curso-lenguaje-python/dia_11/03_web_scraping_03.py

42 lines
762 B
Python
Raw Normal View History

2023-04-12 21:59:47 +02:00
"""
Web scraping básico
Con las librerias beautifulsoup4, lxml y requests
"""
import bs4
import requests
# Variables
2023-04-12 22:47:39 +02:00
url_base = 'http://books.toscrape.com/catalogue/page-{}.html'
fin_url = True
page = 0
lista_titulos = []
2023-04-12 21:59:47 +02:00
# Bucle para formar url y añadir a una lista
while fin_url:
2023-04-12 21:59:47 +02:00
page += 1
page = str(page)
2023-04-12 21:59:47 +02:00
2023-04-12 22:47:39 +02:00
enlace = url_base.format(page)
2023-04-12 21:59:47 +02:00
resultado = requests.get(enlace)
sopa = bs4.BeautifulSoup(resultado.text, 'lxml')
2023-04-12 21:59:47 +02:00
if resultado:
page = int(page)
2023-04-12 21:59:47 +02:00
# Todos los títulos
titulos = sopa.select('.product_pod a')
2023-04-12 22:47:39 +02:00
for title in titulos:
if title.get('title') != None:
lista_titulos.append(title.get('title'))
2023-04-12 21:59:47 +02:00
else:
fin_url = False
2023-04-12 22:47:39 +02:00
print(lista_titulos)