Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何获取文本?_Python_Beautifulsoup - Fatal编程技术网

Python 如何获取文本?

Python 如何获取文本?,python,beautifulsoup,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;&am

例如:

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

1.每二次
多项式的
最多可以有
(a) 如果
b2–4ac是一个完美的正方形,根是有理的。

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

通过升级您制作的内容:

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

试试看。虽然我使用了您提供的内容中的类名,但它们似乎是动态生成的,因此不值得依赖

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)

您尝试了什么?对于mcq.find_all('div',class=“col-md-8”)中的b,您应该包含您尝试过的代码的最低版本,但在您的答案中不起作用,我们非常感谢它,它使您的帮助更加容易。更多信息:澄清“仅包含强标记的div”:你是指只包含强标记的div,还是仅包含强标记的div?我看到这可能是多行书写的,以更好的注释性,但这在列表理解中可能更优雅。你是对的@D.Everhard故意(太)长;)
Every quadratic polynomial can have at most