Convertir una página web en datos con BeautifulSoup: obtener los hipervínculos
En este ejercicio, descubrirás cómo extraer las URL de los hipervínculos de la página web de BDFL. En el proceso, te harás muy amigo del método de la sopa find_all()
.
Este ejercicio forma parte del curso
Intermedio Importar datos en Python
Instrucciones de ejercicio
- Utilice el método
find_all()
para encontrar todos los hipervínculos ensoup
, recordando que los hipervínculos están definidos por la etiqueta HTML<a>
but passed tofind_all()
without angle brackets; store the result in the variablea_tags
. - The variable
a_tags
is a results set: your job now is to enumerate over it, using afor
loop and to print the actual URLs of the hyperlinks; to do this, for every elementlink
ina_tags
, you want toprint()
link.get('href')
.
Ejercicio interactivo práctico
Pruebe este ejercicio completando este código de muestra.
# Import packages
import requests
from bs4 import BeautifulSoup
# Specify url
url = 'https://www.python.org/~guido/'
# Package the request, send the request and catch the response: r
r = requests.get(url)
# Extracts the response as html: html_doc
html_doc = r.text
# create a BeautifulSoup object from the HTML: soup
soup = BeautifulSoup(html_doc)
# Print the title of Guido's webpage
print(soup.title)
# Find all 'a' tags (which define hyperlinks): a_tags
# Print the URLs to the shell
for ____ in ____:
____