I am getting all accurences of a specific tag (td) with find_all() but I want to handle different classes. My tags look something like this:
<td class="classname" colspan="2">
Something ...
</td>
I want to do different things depending on the classname.
I tried using has_attr() but it only seems to check if there is a single attribute present and does not work for whole expressions.
for tag in soup.find_all("td"):
if tag.has_attr("td", {"class": "search-detail-heading"}):
Do Something()
if tag.has_attr("td", {"class": "search-detail-subject"}):
Do Something else()
Is there another method to check for this classname? Do I need to adjust find_all()? I kinda don't want to use find_all() for one classtype and then again for the next classtype.
CodePudding user response:
You can return the td
tag attributes information as a list using this code:
for tag in soup.find_all("td"):
print(list(tag.find("div").attrs.keys()))