tarek hassan
tarek hassan

Reputation: 802

How to extract the child of a tag in Beautifulsoup?

I have the following code and I want to extract content inside

<p><strong>1. Start big</strong><br><br>
Make a slam dunk right away. Boom! Just do it! Start strong! If you’re making a list article about poodle outerwear, don’t save the best for last: put that sporty little pool-vest idea right up there at the top. </p>

<p><strong>2. Hook them and hook them good</strong><br><br>
A recent study of lists (included in another article about the top ten research studies, natch), assembled by some guy you’ve never heard of from an obscure European university in his spare time, found that Web readers usually don’t make it past the first few items on a list. Sad, isn’t it? I bet you’re already thinking about stopping. Yes, it sucks to know people have shorter attention spans than an overly-caffeinated Himalayan fruit-fly. Make the first few count, okay?</p>

<p><strong>3. Stay on message</strong><br><br>
Let’s say you’re writing a list article about the top movies starring Naomi Watts that don’t suck. It’s a short list, if you remember anything about King Kong or her early indie films. I see this kind of thing pop up on <a href="http://www.foxnews.com" rel="nofollow">Fox News</a> and <a href="http://www.metacritic.com" rel="nofollow">Metacritic</a> once in awhile, and I usually can’t stop myself from clicking on them. You get into sort of a click-trance. In fact, hang on a second. I think there might be one on the top opening acts when The Bieb performs in space. Oh yes there is! Okay, back. So, in your article list of the top movies that use a Meatloaf song in the soundtrack, adding that one from Black Sabbath is just not proper usage. We want Meatloaf and Meatloaf only, people! Besides, Black Sabbath is for sissies.</p>

my code in Python to extract p tags is

soup = BeautifulSoup(page, "lxml")

for content in soup.find_all('p'):
    print(content)

what should I add to it to extract strong?

I already tried soup.find_all('p > strong')

Upvotes: 1

Views: 161

Answers (1)

cssko
cssko

Reputation: 3045

from bs4 import BeautifulSoup

page = """
<p><strong>1. Start big</strong><br><br>
Make a slam dunk right away. Boom! Just do it! Start strong! If you’re making a list article about poodle outerwear, don’t save the best for last: put that sporty little pool-vest idea right up there at the top. </p>

<p><strong>2. Hook them and hook them good</strong><br><br>
A recent study of lists (included in another article about the top ten research studies, natch), assembled by some guy you’ve never heard of from an obscure European university in his spare time, found that Web readers usually don’t make it past the first few items on a list. Sad, isn’t it? I bet you’re already thinking about stopping. Yes, it sucks to know people have shorter attention spans than an overly-caffeinated Himalayan fruit-fly. Make the first few count, okay?</p>

<p><strong>3. Stay on message</strong><br><br>
Let’s say you’re writing a list article about the top movies starring Naomi Watts that don’t suck. It’s a short list, if you remember anything about King Kong or her early indie films. I see this kind of thing pop up on <a href="http://www.foxnews.com" rel="nofollow">Fox News</a> and <a href="http://www.metacritic.com" rel="nofollow">Metacritic</a> once in awhile, and I usually can’t stop myself from clicking on them. You get into sort of a click-trance. In fact, hang on a second. I think there might be one on the top opening acts when The Bieb performs in space. Oh yes there is! Okay, back. So, in your article list of the top movies that use a Meatloaf song in the soundtrack, adding that one from Black Sabbath is just not proper usage. We want Meatloaf and Meatloaf only, people! Besides, Black Sabbath is for sissies.</p>
"""

soup = BeautifulSoup(page, 'lxml')

for content in soup.select('p > strong'):
    print(content)

Output:

<strong>1. Start big</strong>
<strong>2. Hook them and hook them good</strong>
<strong>3. Stay on message</strong>

You need to use the .select method for CSS selectors, not the .find

You can find the bs4 docs on .select here, and some CSS selector docs from w3schools here.

Upvotes: 1

Related Questions