Challenge: Attributes
Uppgift
Swipe to start coding
You will work on the page about Christ the Redeemer as in the previous task.
- Print the attributes of the first
<p>
tag. - Print only the text of the first
<ul>
tag.
Lösning
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Import libraries
from bs4 import BeautifulSoup
from urllib.request import urlopen
# Open the 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")
# Create the BeautifulSoup object
soup = BeautifulSoup(html, "html.parser")
# Print attributes of the p tag
print(soup.p.attrs)
# Print only the text of the ul tag
print(soup.ul.get_text())
Var allt tydligt?
Tack för dina kommentarer!
Avsnitt 3. Kapitel 2
single
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Import libraries
from bs4 import BeautifulSoup
from urllib.request import urlopen
# Open the 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")
# Create the BeautifulSoup object
soup = BeautifulSoup(html, "html.parser")
# Print attributes of the p tag
___
# Print only the text of the ul tag
___
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal