Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Navigating HTML Document | Decoding HTML with Beautiful Soup
Web Scraping with Python

bookNavigating HTML Document

After reading the HTML document, you can navigate it in different ways. To explore it further, specify a tag as an attribute. For example, examine the <head> element and display it in a structured format using the .prettify() method.

123456789101112
# Importing libraries from bs4 import BeautifulSoup from urllib.request import urlopen # Reading web page url = "https://codefinity-content-media.s3.eu-west-1.amazonaws.com/18a4e428-1a0f-44c2-a8ad-244cd9c7985e/jesus.html" page = urlopen(url) html = page.read().decode("utf-8") # Reading HTML with BeautifulSoup soup = BeautifulSoup(html, "html.parser") print(soup.head.prettify())
copy

Feel free to experiment by substituting the .head attribute with .body, for example. As shown above, the <head> element encompasses several children. You can iterate through all the children of elements using a for loop and the .children attribute.

1234567891011121314
# Importing libraries from bs4 import BeautifulSoup from urllib.request import urlopen # Reading web page url = "https://codefinity-content-media.s3.eu-west-1.amazonaws.com/18a4e428-1a0f-44c2-a8ad-244cd9c7985e/jesus.html" page = urlopen(url) html = page.read().decode("utf-8") # Reading HTML with BeautifulSoup soup = BeautifulSoup(html, "html.parser") # Iterating over all element children for child in soup.head.children: print(child)
copy
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain what the `.children` attribute does in BeautifulSoup?

How can I access other elements besides `<head>` using BeautifulSoup?

What is the difference between `.children` and `.contents` in BeautifulSoup?

Awesome!

Completion rate improved to 4.35

bookNavigating HTML Document

Swipe to show menu

After reading the HTML document, you can navigate it in different ways. To explore it further, specify a tag as an attribute. For example, examine the <head> element and display it in a structured format using the .prettify() method.

123456789101112
# Importing libraries from bs4 import BeautifulSoup from urllib.request import urlopen # Reading web page url = "https://codefinity-content-media.s3.eu-west-1.amazonaws.com/18a4e428-1a0f-44c2-a8ad-244cd9c7985e/jesus.html" page = urlopen(url) html = page.read().decode("utf-8") # Reading HTML with BeautifulSoup soup = BeautifulSoup(html, "html.parser") print(soup.head.prettify())
copy

Feel free to experiment by substituting the .head attribute with .body, for example. As shown above, the <head> element encompasses several children. You can iterate through all the children of elements using a for loop and the .children attribute.

1234567891011121314
# Importing libraries from bs4 import BeautifulSoup from urllib.request import urlopen # Reading web page url = "https://codefinity-content-media.s3.eu-west-1.amazonaws.com/18a4e428-1a0f-44c2-a8ad-244cd9c7985e/jesus.html" page = urlopen(url) html = page.read().decode("utf-8") # Reading HTML with BeautifulSoup soup = BeautifulSoup(html, "html.parser") # Iterating over all element children for child in soup.head.children: print(child)
copy
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 2
some-alt