Thoms
Thoms

Reputation: 103

BeautifulSoup: extract nth child in a list

i'm looking to extract "What i want" from an html list with BeautifulSoup.

<ul class="two-columns">
    <li class="two-columns_a">
        <span> Something1: </span><span class="red">What I want</span>
    </li>
    <li class="two-columns_a">
        <span> Something2: </span><span class="red">What I want</span>
    </li>
    <li class="two-columns_a">
    <span> Something3: </span><span class="red">What I want</span>
    </li> 
</ul>

So far, I did something like this, but it returns both values between span tag.

data['something1'] = soup.select("li.two-columns_a")[0].text.strip() 
data['something2'] = soup.select("li.two-columns_a")[1].text.strip()
data['something3'] = soup.select("li.two-columns_a")[2].text.strip()

Thanks in advance for your help,

Upvotes: 1

Views: 310

Answers (1)

Jack Fleeting
Jack Fleeting

Reputation: 24940

Assuming data is intended as a dictionary, you can try:

data= {}
for target in soup.select('ul.two-columns li.two-columns_a'):
    data[target.select_one('span:nth-child(1)').text]=(target.select_one('span:nth-child(2)').text)
data

Output should be:

{' Something1: ': 'What I want',
 ' Something2: ': 'What I want',
 ' Something3: ': 'What I want'}

Upvotes: 1

Related Questions