且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用python解析html文件并获取标签之间的文本?

更新时间:2023-02-19 17:07:57

以下是使用 BeautifulSoup的示例解析HTML:

Here is an example of using BeautifulSoup to parse HTML:

from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup("""<html><body>
                        <div id="a" class="c1">
                            We want to get this
                        </div>
                        <div id="b">
                            We don't want to get this
                        </div></body></html>""")
print soup('div', id='a').text

此输出

We want to get this