Aks
Aks

Reputation: 952

Beautiful Soup Access the second <div with the same class

I'm scraping an html document that contains two 'hooks' of the same class like below:

<div class="multiRow">
    <!--ModuleId 372329FileName @swMultiRowsContainer-->
    <some more content>
</div>
<div class="multiRow">
    <!--ModuleId 372330FileName @multiRowsContainer-->
    <some more content>
</div>

When I do:

mr = ct[0].find_all('div', {'class': 'multiRow'})

I only get contents from the first Is there a way to get access to contents within the second ?

Thanks!

Upvotes: 1

Views: 19401

Answers (3)

Perman27
Perman27

Reputation: 11

Because you are asking for the first content only, check your code

ct[0].find_all

The ct[0] will grab only the first content, not the whole. Fix that.

Upvotes: 0

Jayantha
Jayantha

Reputation: 2349

Coding example for Adam Smith's comment. I think it is very clear.

ct= soup.findAll("div", {"class" : "multiRow"})
ct= ct[1]
print(ct)

Upvotes: 0

sihrc
sihrc

Reputation: 2828

Edit with Adam Smith's comment.

Refer to my comment above, code below:

from bs4 import BeautifulSoup as soup
a = "<div class=\"multiRow\"><!--ModuleId 372329FileName @swMultiRowsContainer-->Bye</div>    <div class=\"multiRow\"><!--ModuleId 372330FileName @multiRowsContainer-->Hi</div>"

print soup(a).find_all("div",{"class":"multiRow"})[1]

returns:

<div class="multiRow"><!--ModuleId 372330FileName @multiRowsContainer-->Hi</div>

Upvotes: 5

Related Questions