如何获取文字?

时间:2017-11-09 13:54:30

标签: python beautifulsoup

示例:

<div class=" col-md-8">
   <strong>1.</strong>&nbsp;&nbsp;&nbsp;&nbsp;Every quadratic 
   polynomial 
 can have at most
 </div>
  <div class=" col-md-
   8">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(a) If 
   b<sup>2</sup> – 4ac is a perfect square, the roots are rational.
   </div>

如何使用find_all方法获取仅包含强标记的div文本?

2 个答案:

答案 0 :(得分:0)

通过升级你所做的一点:

results = []
for b in mcq.find_all('div',class_=" col-md-8"): #here you find all the div of the correct class
    if b and b.find_all("strong"): # then you check if there is a "strong" tag inside 
        results.append(b) # if yes => append it to the final list

答案 1 :(得分:0)

放手一搏。虽然我使用了您提供的内容中的类名,但它们并不依赖于它们似乎动态生成的内容。

from bs4 import BeautifulSoup

html_content='''
<div class=" col-md-8">
        <strong>1.</strong>&nbsp;&nbsp;&nbsp;&nbsp;Every quadratic 
        polynomial 
        can have at most
    </div>
    <div class=" col-md-
        8">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(a) If 
        b<sup>2</sup> – 4ac is a perfect square, the roots are rational.
</div>
'''
soup = BeautifulSoup(html_content,"lxml")
for items in soup.find_all("div",class_="col-md-8")[0].find_all("strong"):
    core_text = ' '.join([item.strip() for item in items.next_sibling.split()])
    print(core_text)

输出:

Every quadratic polynomial can have at most