【问题标题】:find_all() returns only first item of the listfind_all() 仅返回列表的第一项
【发布时间】:2019-08-01 13:35:00
【问题描述】:

我在使用 BeautifulSoup 时遇到了一些问题,使用 find_all() 方法。我正在尝试在所有 p 标签之间获取文本,但它只返回列表的第一个元素。实际上列表只有一项。为什么 find_all() 方法只返回一项?

这是我要提取的代码的一部分:

<div class="post-content">
 <p>If you’re not familiar with Deep Image, it’s an amazing tool which allows you to increase the size of an image and upgrade its quality at the same time.</p>

 <p>You can find it, and use for free <a href="https://deep-image.ai/">HERE</a></p>

 <p><em>The goal of this blog post is to focus on the main changes and showcase the results of DI 2.0 algorithms.</em></p>

 <p>As we all know a picture is worth a thousand words. So we will let the enhanced pictures speak for themselves. All pictures you can see below were processed using Deep Image algorithms.</p>

 <h2 id="what-has-changed">What has changed</h2>

 <p>Here are all the main improvements added to Deep Image 2.0:</p>
</div>

这是我的代码:

from bs4 import BeautifulSoup
import requests

source = requests.get('https://teonite.com/blog/deep-image-2-showcasing-results/').text
soup = BeautifulSoup(source, 'html.parser')

for article in soup.find_all(class_='post-content'):
    print(article.p.text)

感谢您的帮助!

【问题讨论】:

  • 样本输入只有一项具有所请求的类。

标签: python beautifulsoup


【解决方案1】:

命令print(article.p.text) 将仅查找并打印第一个&lt;p&gt; 标记。要获取整篇文章的文本,您可以使用例如get_text()

from bs4 import BeautifulSoup
import requests

source = requests.get('https://teonite.com/blog/deep-image-2-showcasing-results/')
soup = BeautifulSoup(source.content, 'html.parser')

for article in soup.find_all(class_='post-content'):
    print(article.get_text(strip=True, separator='\n'))

打印:

If you’re not familiar with Deep Image, it’s an amazing tool which allows you to increase the size of an image and upgrade its quality at the same time.
You can find it, and use for free
HERE
The goal of this blog post is to focus on the main changes and showcase the results of DI 2.0 algorithms.
As we all know a picture is worth a thousand words. So we will let the enhanced pictures speak for themselves. All pictures you can see below were processed using Deep Image algorithms.
What has changed
Here are all the main improvements added to Deep Image 2.0:
You are now able to use a new algorithm to magnify the image two-fold and four-fold. It is based on Generative Adversarial Networks.
The quality of the algorithm has been improved - there are less artefacts and even smoother edges in the enhanced images.
We have delivered a new, more reliable asynchronous queue architecture and task processing, based on microservices.
You can now enjoy a fully redesigned web application
A few examples
Please keep in mind that Deep Image was trained to do a very particular job - it will increase the size of the picture as well as improve its quality once it’s enlarged. It will not improve the quality of the image you have resized before.
Check out those awesome results!
The improvements are clearly visible.
Think about all those old photos you will be able to enhance!
You are the main reason we’re working on those cool projects, so we’d love to get your feedback.
Contact us
and let us know what you think!

注意:

要正确解码字符串,请使用result.content 而不是result.text

【讨论】:

    【解决方案2】:

    您正在搜索类为post-content 的所有标签。虽然只有一个这样的元素,find_all 返回一个包含单个条目的列表。因此,您的 for 循环中只有一次迭代,并且您仅打印此迭代中第一个 p 标记的文本。

    试试这个:

    from bs4 import BeautifulSoup
    import requests
    
    html = '''
    <div class="post-content">
     <p>If you’re not familiar with Deep Image, it’s an amazing tool which allows you to increase the size of an image and upgrade its quality at the same time.</p>
    
     <p>You can find it, and use for free <a href="https://deep-image.ai/">HERE</a></p>
    
     <p><em>The goal of this blog post is to focus on the main changes and showcase the results of DI 2.0 algorithms.</em></p>
    
     <p>As we all know a picture is worth a thousand words. So we will let the enhanced pictures speak for themselves. All pictures you can see below were processed using Deep Image algorithms.</p>
    
     <h2 id="what-has-changed">What has changed</h2>
    
     <p>Here are all the main improvements added to Deep Image 2.0:</p>
    </div>
    '''
    
    soup = BeautifulSoup(html, 'html.parser')
    div = soup.find(class_='post-content')
    for p in div.find_all('p'):
        print(p.text)
    

    您将获得p 标签内所有文本的所需输出,因为我们现在搜索具有post-content 类的元素,然后搜索该元素内的所有p 标签。

    【讨论】:

      猜你喜欢
      • 2021-10-01
      • 1970-01-01
      • 2019-12-12
      • 2019-03-25
      • 2022-01-08
      • 1970-01-01
      • 2016-08-04
      • 2021-12-22
      • 1970-01-01
      相关资源
      最近更新 更多