简体   繁体   English

使用 Soup 获取标签名称和文本

[英]Get tag name and text with Soup

I try to get the text from a header with BeautifulSoup. The header is dynamic in his attributes.我尝试从 header 和 BeautifulSoup 获取文本。header 在他的属性中是动态的。 That is the reason, why I minimize the html-code.这就是为什么我最小化 html 代码的原因。 I want to get the text and the according tag-name.我想获取文本和相应的标签名称。 I tried this:我试过这个:

 for element in header.find_all(string = True):
                
                if len(element.text.strip('\n')) > 0:
                    print(element)
                    a = header.find_all(text = element.text)
                    print(a + " " + a.text.strip('\n'))

But the print output is only the text and not the tag with text.但是打印 output 只是文本而不是带有文本的标签。 How can I solve that?我该如何解决?

Thank you in forward.谢谢你。

First:第一的:

<header>

    <div>
        <h2>
            <span>
                Text A here</span>
            <span><span>Text B Here <span>
                    </span>
        </h2>
        <div>
            <div>
                <span>
                    <div>
                    </div>
                </span>
            </div>
            <div>
                Text C Here
                <a>
                    Text C Here</a>
            </div>
        </div>
        <div>

            <a>
                Text D</a>
        </div>
        <div>
            Text E
        </div>
    </div>
    </div>
</header>

Second Example:第二个例子:

<header>
    <div>
        <div>
            <h2>
                <span>
                    Text A
                </span>
                <span><span>Text B</span>
                </span>
            </h2>
            <div>
                Text C
            </div>
        </div>
    </div>
</header>

3th Example:第三个例子:

<header>
    <div>
        <div>
            <h2>
                <span>
                    Text A
                </span>
                <span>
                    <span>
                        <span><svg>
                                <g>
                                    <rect></rect>
                                    <path></path>
                                    <path></path>
                                </g>
                            </svg>
                        </span>
                    </span><span>Text B</span>
                </span>
            </h2>
            <div>
                Text C
            </div>
            <div>
                Text D
                <a>
                    Text E</a><span>Text F </span>
                <a>
                    Text G</a><span> Text H </span>
                <a>
                    Text I</a>
            </div>
        </div>
    </div>
</header>

Use element.parent.name to get the tag name.使用element.parent.name获取标签名称。

Based on your example 1:根据您的示例 1:

    from bs4 import BeautifulSoup

    html='''<header>
    
        <div>
            <h2>
                <span>
                    Text A here</span>
                <span><span>Text B Here <span>
                        </span>
            </h2>
            <div>
                <div>
                    <span>
                        <div>
                        </div>
                    </span>
                </div>
                <div>
                    Text C Here
                    <a>
                        Text C Here</a>
                </div>
            </div>
            <div>
    
                <a>
                    Text D</a>
            </div>
            <div>
                Text E
            </div>
        </div>
        </div>
    </header>'''
    
    header=BeautifulSoup(html.strip(), 'html.parser')
    for element in header.find_all(string = True):
                    
        if len(element.text.strip('\n')) > 0:
            print(element.strip())
            a = header.find(text = element.text)
            print(a.parent.name + " " + a.text.strip())

Output: Output:

Text A here
span Text A here
Text B Here
span Text B Here
Text C Here
div Text C Here
Text C Here
a Text C Here
Text D
a Text D
Text E
div Text E

Example 3:示例 3:

html='''<header>
    <div>
        <div>
            <h2>
                <span>
                    Text A
                </span>
                <span>
                    <span>
                        <span><svg>
                                <g>
                                    <rect></rect>
                                    <path></path>
                                    <path></path>
                                </g>
                            </svg>
                        </span>
                    </span><span>Text B</span>
                </span>
            </h2>
            <div>
                Text C
            </div>
            <div>
                Text D
                <a>
                    Text E</a><span>Text F </span>
                <a>
                    Text G</a><span> Text H </span>
                <a>
                    Text I</a>
            </div>
        </div>
    </div>
</header>'''

header=BeautifulSoup(html.strip(), 'html.parser')
for element in header.find_all(string = True):
                
    if len(element.text.strip('\n')) > 0:
        print(element.strip())
        a = header.find(text = element.text)
        print(a.parent.name + " " + a.text.strip())

Output: Output:

Text A
span Text A
Text B
span Text B
Text C
div Text C
Text D
div Text D
Text E
a Text E
Text F
span Text F
Text G
a Text G
Text H
span Text H
Text I
a Text I

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM