BeautifulSoupを使用して、指定された属性などではなく、指定された属性のみでタグを一致させる方法はありますか? たとえば、次の単純な HTML では:classclass
<html>
<head>
<title>
Title here
</title>
</head>
<body>
<div class="one two">
some content here
</div>
<div class="two">
more content here
</div>
</body>
</html>
withのみを一致させ、divwithを一致さclass="two"せないことはできますか? 何かが欠けていない限り、ドキュメントのそのセクションからは何のアイデアも得られません。これは私が現在使用しているコードです:divclass="one two"
from bs4 import BeautifulSoup
html = '''
<html>
<head>
<title>
Title here
</title>
</head>
<body>
<div class="one two">
should not be matched
</div>
<div class="two">
this should be matched
</div>
</body>
</html>
'''
soup = BeautifulSoup(html)
div_two = soup.find("div", "two")
print(div_two.contents[0].strip())
this should be matchedの代わりにこれを印刷しようとしていますshould not be matched。
編集: この単純な例では、クラスの唯一のオプションが"one two"or"two"であることを知っていますが、実稼働コードでは、一致させたいものに class があることしかわかりません"two"。他のタグには、 に加えて、未知の可能性がある多数の他のクラスが"two"ある可能性があります。