Issue
This is the html code:
<div class="main_class">
<a id = "link_id" href = "link1"></a>
<a id = "link_id" href = "link2"></a>
<a id = "link_id" href = "link3"></a>
<a id = "link_id" href = "link4"></a>
</div>
All the id
's have the same name
I tried using beautiful soup to extract all the a tags
and id
under a specific div
These are the methods I used:
filtered = soup.find_all("div[class=main_class")
filtered = soup.find_all("div", {"id": "link_id"})
filtered = soup.find_all('id', href = True) # Returns extra links which are not in the specific div
I have tried some other methods but I am unable to get all the id
and links
inside a specific div
Solution
Try this:
links = soup.find("div", {"class": "main_class"}).findChildren("a", {"id": "link_id"})
>>> links
[<a href="link1" id="link_id"></a>,
<a href="link2" id="link_id"></a>,
<a href="link3" id="link_id"></a>,
<a href="link4" id="link_id"></a>]
Answered By - Corralien
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.