• <legend id='xwivf'><style id='xwivf'><dir id='xwivf'><q id='xwivf'></q></dir></style></legend>
    • <bdo id='xwivf'></bdo><ul id='xwivf'></ul>
    <i id='xwivf'><tr id='xwivf'><dt id='xwivf'><q id='xwivf'><span id='xwivf'><b id='xwivf'><form id='xwivf'><ins id='xwivf'></ins><ul id='xwivf'></ul><sub id='xwivf'></sub></form><legend id='xwivf'></legend><bdo id='xwivf'><pre id='xwivf'><center id='xwivf'></center></pre></bdo></b><th id='xwivf'></th></span></q></dt></tr></i><div id='xwivf'><tfoot id='xwivf'></tfoot><dl id='xwivf'><fieldset id='xwivf'></fieldset></dl></div>

      <small id='xwivf'></small><noframes id='xwivf'>

      <tfoot id='xwivf'></tfoot>

        Beautifulsoup 通过 &lt;br/&gt; 分割标签中的文本

        Beautifulsoup split text in tag by lt;br/gt;(Beautifulsoup 通过 lt;br/gt; 分割标签中的文本)

                • <bdo id='TEel7'></bdo><ul id='TEel7'></ul>
                • <small id='TEel7'></small><noframes id='TEel7'>

                  <tfoot id='TEel7'></tfoot>
                  <i id='TEel7'><tr id='TEel7'><dt id='TEel7'><q id='TEel7'><span id='TEel7'><b id='TEel7'><form id='TEel7'><ins id='TEel7'></ins><ul id='TEel7'></ul><sub id='TEel7'></sub></form><legend id='TEel7'></legend><bdo id='TEel7'><pre id='TEel7'><center id='TEel7'></center></pre></bdo></b><th id='TEel7'></th></span></q></dt></tr></i><div id='TEel7'><tfoot id='TEel7'></tfoot><dl id='TEel7'><fieldset id='TEel7'></fieldset></dl></div>
                    <tbody id='TEel7'></tbody>

                • <legend id='TEel7'><style id='TEel7'><dir id='TEel7'><q id='TEel7'></q></dir></style></legend>
                  本文介绍了Beautifulsoup 通过 &lt;br/&gt; 分割标签中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  是否可以通过 br 标签从标签中拆分文本?

                  Is it possible to split a text from a tag by br tags?

                  我有这个标签内容:[u'+420 777 593 531', <br/>, u'+420 776 593 531', <br/>, u'+420 775593 531']

                  我只想得到数字.有什么建议吗?

                  And I want to get only numbers. Any advices?

                  [x for x in dt.find_next_sibling('dd').contents if x!=' <br/>']
                  

                  根本不工作.

                  推荐答案

                  您需要测试 标签,这些标签被建模为 Element 实例.Element 对象具有 name 属性,而文本元素没有(它们是 NavigableText 实例):

                  You need to test for tags, which are modelled as Element instances. Element objects have a name attribute, while text elements don't (which are NavigableText instances):

                  [x for x in dt.find_next_sibling('dd').contents if getattr(x, 'name', None) != 'br']
                  

                  由于您在该 <dd> 元素中似乎只有文本和 <br/> 元素,因此您不妨只获取 所有包含的字符串 改为:

                  Since you appear to only have text and <br /> elements in that <dd> element, you may as well just get all the contained strings instead:

                  list(dt.find_next_sibling('dd').stripped_strings)
                  

                  演示:

                  >>> from bs4 import BeautifulSoup
                  >>> soup = BeautifulSoup('''
                  ... <dt>Term</dt>
                  ... <dd>
                  ...     +420 777 593 531<br/>
                  ...     +420 776 593 531<br/>
                  ...     +420 775 593 531<br/>
                  ... </dd>
                  ... ''')
                  >>> dt = soup.dt
                  >>> [x for x in dt.find_next_sibling('dd').contents if getattr(x, 'name', None) != 'br']
                  [u'
                      +420 777 593 531', u'
                      +420 776 593 531', u'
                      +420 775 593 531', u'
                  ']
                  >>> list(dt.find_next_sibling('dd').stripped_strings)
                  [u'+420 777 593 531', u'+420 776 593 531', u'+420 775 593 531']
                  

                  这篇关于Beautifulsoup 通过 &lt;br/&gt; 分割标签中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)

                  <small id='ZHnW9'></small><noframes id='ZHnW9'>

                  <legend id='ZHnW9'><style id='ZHnW9'><dir id='ZHnW9'><q id='ZHnW9'></q></dir></style></legend>

                    <tbody id='ZHnW9'></tbody>

                      • <tfoot id='ZHnW9'></tfoot>
                        <i id='ZHnW9'><tr id='ZHnW9'><dt id='ZHnW9'><q id='ZHnW9'><span id='ZHnW9'><b id='ZHnW9'><form id='ZHnW9'><ins id='ZHnW9'></ins><ul id='ZHnW9'></ul><sub id='ZHnW9'></sub></form><legend id='ZHnW9'></legend><bdo id='ZHnW9'><pre id='ZHnW9'><center id='ZHnW9'></center></pre></bdo></b><th id='ZHnW9'></th></span></q></dt></tr></i><div id='ZHnW9'><tfoot id='ZHnW9'></tfoot><dl id='ZHnW9'><fieldset id='ZHnW9'></fieldset></dl></div>
                        • <bdo id='ZHnW9'></bdo><ul id='ZHnW9'></ul>