Issue
I was able to get a list of webpage elements with the tag div
and its contents. It contains a list of all available links in a specific div
.
The list looks like this:
# I formatted the list contents to look like an HTML code
classroom_links =
[<div class="main_class">
<div class="sub_class">
<a href="link1" id="id_name"></a>
<a href="link2" id="id_name"></a>
<a href="link3" id="id_name"></a>
<a href="link4" id="id_name"></a>
<a href="link5" id="id_name"></a>
</div>
</div>
]
classroomLinks = soup.find_all("div", {"class": "main_class"})
for links in classroomLinks:
print(links.find('a')['href'])
Output:
`"link1"`
But this prints only the first link. I am unable to print all the remaining links.
Solution
You can try to iterate over a
tags for each element of the list:
for dom in classroom_links:
for aTag in dom.find_all("a"):
print(aTag)
Full example:
from bs4 import BeautifulSoup
classroom_links = [BeautifulSoup("""<div class="main_class">
<div class="sub_class">
<a href="link1" id="id_name"></a>
<a href="link2" id="id_name"></a>
<a href="link3" id="id_name"></a>
<a href="link4" id="id_name"></a>
<a href="link5" id="id_name"></a>
</div>
</div>""")]
for dom in classroom_links:
for aTag in dom.find_all("a"):
print(aTag)
# <a href="link1" id="id_name"></a>
# <a href="link2" id="id_name"></a>
# <a href="link3" id="id_name"></a>
# <a href="link4" id="id_name"></a>
# <a href="link5" id="id_name"></a>
Answered By - Alexandre B.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.