Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Attributes and Contents of Multiple Elements | Working with Element Attributes in Beautiful Soup
Web Scraping with Python

bookAttributes and Contents of Multiple Elements

メニューを表示するにはスワイプしてください

All the methods covered in the previous chapter can be applied to elements with a specific tag, that is, to the result of the .find_all() method. However, since .find_all() returns a list, you must access attributes and methods for each element separately. Use a for loop for this purpose. For example, retrieve all attributes of all <div> elements.

12345678910111213
# 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") for div in soup.find_all("div"): print(div.attrs)
copy

The same approach works for extracting text. For example, get all the text from every <p> element.

12345678910111213
# 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") for p in soup.find_all("p"): print(p.get_text())
copy
すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 3.  3

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 3.  3
some-alt