'Iterate through all tags within found tag
How can I iterate through all tags under the found tag?
This gives me only top level tags
description = soup.find("div", {"class": "description"})
for tag in description:
print(tag)
This gives me iteration until the end of html
description = soup.find("div", {"class": "description"})
while description:
description = description.next_element
print(description)
Solution 1:[1]
description is not iterable, because find() method returns the first selected tag from soup, so comes use the findAll() method.
descriptions = soup.findAll("div", {"class": "description"})
for description in descriptions:
print(description)
Solution 2:[2]
are you looking for .descendants?
description = soup.find("div", {"class": "description"})
for tag in description.descendants:
print(tag)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | chitown88 |
