Навігація HTML-Документом
Після зчитування HTML-документа ви можете навігувати ним різними способами. Для подальшого вивчення вкажіть тег як атрибут. Наприклад, дослідіть елемент <head> і відобразіть його у структурованому вигляді за допомогою методу .prettify().
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())
Можна експериментувати, замінюючи атрибут .head, наприклад, на .body. Як показано вище, елемент <head> містить декілька дочірніх елементів. Ви можете ітерувати всі дочірні елементи за допомогою циклу for та атрибута .children.
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)
Дякуємо за ваш відгук!
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
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
Навігація HTML-Документом
Свайпніть щоб показати меню
Після зчитування HTML-документа ви можете навігувати ним різними способами. Для подальшого вивчення вкажіть тег як атрибут. Наприклад, дослідіть елемент <head> і відобразіть його у структурованому вигляді за допомогою методу .prettify().
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())
Можна експериментувати, замінюючи атрибут .head, наприклад, на .body. Як показано вище, елемент <head> містить декілька дочірніх елементів. Ви можете ітерувати всі дочірні елементи за допомогою циклу for та атрибута .children.
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)
Дякуємо за ваш відгук!