2017-12-04 2 views
1

私は、次のコードを持っていると私は、pタグを抽出するためにBeautifulsoupでタグの子を抽出する方法は?

<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> 

Pythonで私のコード内のコンテンツを抽出したいが、私は強い抽出するために追加すべきか

soup = BeautifulSoup(page, "lxml") 

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

のですか?

私はすでにsoup.find_all('p > strong')

答えて

1
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) 

出力しようとした:あなたは.select上BS4のドキュメントを見つけることができます

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

あなたはCSSセレクタのため.select方法を使用する必要がなく、.find

here、およびw3schoolsの一部のCSSセレクタドキュメントhere

+2

"print(content.find( 'strong'))' – erocoar

+0

特に、あなたが同時に 'content'を使って何か他のことをしたいのであれば – cssko

関連する問題