htmlのclassが以下のものを指定したい。 beautifulsoup product-detail -pc
Beautiful Soupを使用して、class属性が"product-detail"かつ"pc"である要素を取得する方法は以下の通りです。
python
from bs4 import BeautifulSoup # 仮のHTMLコード html_content = """ <html> <body> <div class="product-detail pc">Product 1</div> <div class="product-detail mobile">Product 2</div> <div class="product-detail pc">Product 3</div> </body> </html> """ # BeautifulSoupを使ってHTMLを解析 soup = BeautifulSoup(html_content, 'html.parser') # class属性が"product-detail"かつ"pc"である要素を取得 elements = soup.find_all(class_="product-detail pc") # 結果を出力 for element in elements: print(element.text)
このコードでは、find_allメソッドを使用してclass属性が"product-detail pc"である要素を取得し、そのテキストを出力しています。

ディスカッション
コメント一覧
まだ、コメントがありません