【问题标题】:How to parse "data-text" with beautifulsoup? [duplicate]如何用 Beautifulsoup 解析“数据文本”? [复制]
【发布时间】:2020-11-06 11:55:27
【问题描述】:

有一段HTML代码:

<div class="some div name" data-text="important text">...</div> 

我们需要从“data-text”中获取文本。我试图在 BeautifulSoup 官方文档中找到一些东西,但没有这样的东西(或者我看起来很糟糕)。

【问题讨论】:

  • 1.将您的 html 对象传递给美丽的汤。 Ldata = BeautifulSoup("
    ...
    "); 2. 从 div 中获取您要查找的属性。 ldataText = Ldata.attrs['data-text'];

标签: python html beautifulsoup


【解决方案1】:

您只需在此代码中将 'href' 替换为 'data-text':

html = urlopen("http://kite.com")
text = html.read()
plaintext = text.decode('utf8')
links = re.findall("href=[\"\'](.*?)[\"\']", plaintext)
print(links[:5])

https://kite.com/python/answers/how-to-get-href-links-from-urllib-urlopen-in-python

【讨论】:

    【解决方案2】:

    您可以在标签上使用['data-text'].get('data-text') 来获取属性值。

    例如:

    from bs4 import BeautifulSoup
    
    txt = '''<div class="some div name" data-text="important text">...</div>'''
    soup = BeautifulSoup(txt, 'html.parser')
    
    print(soup.find('div', {'data-text': True})['data-text'])
    

    打印:

    important text
    

    【讨论】:

    • 谢谢!这正是我想要的方式!
    【解决方案3】:

    你可以试试这个。

    from bs4 import BeautifulSoup
    import requests
    ...
    bsObj = BeautifulSoup(html, features = "html.parser")
    div_tag = bsObj.find("div", class_ = "some div name")
    if div_tag:
        data_text = div_tag['data-text']
    print(data_text)
    

    希望对你有帮助。

    【讨论】:

      【解决方案4】:

      你提供的信息这么少,我想不出比这更好的了:

      from bs4 import BeautifulSoup
      
      html = '<div class="some div name" data-text="important text">...</div>'
      soup = BeautifulSoup(html, 'html.parser')
      div = soup.select_one('div.some.div.name')
      print(div.get('data-text'))
      

      输出:

      important text
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-05
        • 1970-01-01
        • 2015-12-31
        • 2021-01-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多