【问题标题】:Getting only some tags <p> using BeautifulSoup from website使用来自网站的 BeautifulSoup 仅获取一些标签 <p>
【发布时间】:2020-08-08 16:44:24
【问题描述】:

我尝试仅从 selectet 标记中获取文本,例如:

<div class="article-container">
  <p>tekst 1</p> <!-- this tag -->
  <p>none</p>
  <p>tekst 2</p> <!-- this tag -->
  <p>none</p>
  <p>tekst 3</p> <!-- this tag -->
  <p>none</p>
  <p>tekst 4</p> <!-- this tag -->
</div>

我尝试获取“tekst 1 tekst 2 tekst 3 tekst 4”(但标签中的文字完全不同,“tekst 1”等只是示例),

我的简单python函数如下所示:

def get_article(url):
    page = requests.get(str(url))
    soup = BeautifulSoup(page.text, 'html.parser')

    article = soup.find(class_='article-container')

    article_only = article.text

    return(article_only)

但他返回了整个文本。有没有办法像上面的例子一样使用 BS 来获取选定的元素?

【问题讨论】:

  • 你想如何选择标签?您只想要p 标签内的文本吗? none 是什么?
  • 首先,article-container 类在您的 html 中不存在。然后,您是否要查找包含单词tekst 的标签,或者您是否想要不为空的标签或最终两个标签中的一个(奇数)
  • 我只想从标签 1、3、5、7 中获取文本,但我在其中添加了评论。
  • 对不起,我更正了我在上述问题中的错误。

标签: python beautifulsoup


【解决方案1】:

所以你只需要 1,3,5,7 元素,你可以这样做:

代码:

from bs4 import BeautifulSoup as soup

html = """<div class="article-intro">
<p>tekst 1</p>
<p>none</p>
<p>tekst 2</p>
<p>none</p>
<p>tekst 3</p>
<p>none</p>
<p>tekst 4</p>
</div>"""

page = soup(html, 'html.parser')
div = page.find('div',{'class':'article-intro'})
ps = div.find_all('p')
for i in range(len(ps)):
    if i % 2 == 0:
        print(ps[i].text)

输出:

tekst 1
tekst 2
tekst 3
tekst 4

【讨论】:

  • 这个问题目前还不是很清楚,如果他正在寻找奇怪的标签,你的答案可能是一个很好的答案
【解决方案2】:

使用正则表达式re 并搜索文本。

from bs4 import BeautifulSoup
import re

html='''<div class="article-intro">
<p>tekst 1</p>
<p>none</p>
<p>tekst 2</p>
<p>none</p>
<p>tekst 3</p>
<p>none</p>
<p>tekst 4</p>
</div>'''

soup=BeautifulSoup(html,'html.parser')
for item in soup.find('div', class_='article-intro').find_all('p', text=re.compile('tekst')):
    print(item.text)

输出:

tekst 1
tekst 2
tekst 3
tekst 4

或者你可以使用pythonlambda函数。

from bs4 import BeautifulSoup

html='''<div class="article-intro">
<p>tekst 1</p>
<p>none</p>
<p>tekst 2</p>
<p>none</p>
<p>tekst 3</p>
<p>none</p>
<p>tekst 4</p>
</div>'''

soup=BeautifulSoup(html,'html.parser')
for item in soup.find('div', class_='article-intro').find_all(lambda tag:tag.name=='p' and 'tekst' in tag.text):
    print(item.text)

输出:

tekst 1
tekst 2
tekst 3
tekst 4

【讨论】:

    【解决方案3】:

    一些不同的选项取决于您将来真正想做的事情。使用 bs4 4.7.1。

    from bs4 import BeautifulSoup as bs
    
    html = '''
    <div class="article-container">
      <p>tekst 1</p> <!-- this tag -->
      <p>none</p>
      <p>tekst 2</p> <!-- this tag -->
      <p>none</p>
      <p>tekst 3</p> <!-- this tag -->
      <p>none</p>
      <p>tekst 4</p> <!-- this tag -->
    </div>
    '''
    
    soup = bs(html, 'lxml')
    #odd indices
    items = [item.text for item in soup.select('.article-container p:nth-child(odd)')]
    print(items)
    
    #excluding None
    items = [item.text for item in soup.select('.article-container p:not(:contains("none"))')]
    print(items)
    
    #including tekst
    items = [item.text for item in soup.select('.article-container p:contains("tekst")')]
    print(items)
    
    #providing nth list
    items = [item.text for item in soup.select('.article-container p:nth-of-type(1), .article-container p:nth-of-type(3), .article-container p:nth-of-type(5), .article-container p:nth-of-type(7)')]
    print(items)
    

    【讨论】:

      【解决方案4】:

      find_all() 函数总是返回一个列表。

      注意:text 参数是一个旧名称,从 BeautifulSoup 4.4.0 开始,它被称为 string

      虽然 string 是用来查找字符串的,但你可以将它与 查找标签的参数:Beautiful Soup 将查找所有标签 .string 与您的字符串值匹配。此代码查找其标签 .string 是“tekst”:

      from bs4 import BeautifulSoup 
      import re  
      
      html = '''<div class="article-container">
      <p>tekst 1</p>
      <p>none</p>
      <p>tekst 2</p>
      <p>none</p>
      <p>tekst 3</p>
      <p>te</p>
      <p>tekst 4</p>
      </div>'''
      
      soup = BeautifulSoup(html, 'lxml')
      article = soup.select("div[class='article-container']")[0]
      article_only = article.find_all(string=re.compile("tekst"))
      print(article_only)
      

      O/P:

      ['tekst 1', 'tekst 2', 'tekst 3', 'tekst 4']
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-02
        • 1970-01-01
        • 2010-10-06
        • 1970-01-01
        • 1970-01-01
        • 2022-12-09
        • 2019-09-03
        • 2014-10-02
        相关资源
        最近更新 更多