Навігація 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 .prettify() method does?
How can I iterate over the children of the <body> element instead?
What other attributes or methods can I use to navigate the HTML structure?
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)
Дякуємо за ваш відгук!