In [1]: from bs4 import BeautifulSoup In [2]: text = ''' ...: <div> ...: <ul> ...: <li class="item-0" id="first"><a href="https://www.cnblogs.com/Elite-Wang/p/link1.html">first item</a></li> ...: <li class="item-1"><a href="https://www.cnblogs.com/Elite-Wang/p/link2.html">second item</a></li> ...: <li class="item-inactive"><a href="https://www.cnblogs.com/Elite-Wang/p/link3.html"><span class="bold">third item</span></a></li> ...: <li class="item-1"><a href="https://www.cnblogs.com/Elite-Wang/p/link4.html">fourth item</a></li> ...: <li class="item-0"><a href="https://www.cnblogs.com/Elite-Wang/p/link5.html">fifth item</a></li> ...: </ul> ...: </div> ...: ''' In [3]: bs = BeautifulSoup(text)#创建BeautifulSoup对象,可以直接传入字符串到此这篇bs4解析html(bs4解析数据)的文章就介绍到这了,更多相关内容请继续浏览下面的相关推荐文章,希望大家都能在编程的领域有一番成就!
In [4]: bs1 = BeautifulSoup(open('https://www.cnblogs.com/Elite-Wang/p/test.html'))#也可以传入文件对象
In [5]: bs Out[5]: <html><body><div> <ul> <li class="item-0" id="first"><a href=https://www.cnblogs.com/Elite-Wang/p/"link1.html">first item</a></li> <li class="item-1"><a href=https://www.cnblogs.com/Elite-Wang/p/"link2.html">second item</a></li> <li class="item-inactive"><a href=https://www.cnblogs.com/Elite-Wang/p/"link3.html"><span class="bold">third item</span></a></li> <li class="item-1"><a href=https://www.cnblogs.com/Elite-Wang/p/"link4.html">fourth item</a></li> <li class="item-0"><a href=https://www.cnblogs.com/Elite-Wang/p/"link5.html">fifth item</a></li> </ul> </div> </body></html>
版权声明:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权、违法违规、事实不符,请将相关资料发送至xkadmin@xkablog.com进行投诉反馈,一经查实,立即处理!
转载请注明出处,原文链接:https://www.xkablog.com/sjkxydsj/48815.html