BeautifulSoup解析库详解
发布时间:2020-12-14 06:18:34 所属栏目:百科 来源:网络整理
导读:BeautifulSoup是灵活又方便的网页解析库,处理高效,支持多种解析器 利用它不用编写正则表达式即可方便地实现网页信息的提取 安装:pip3 install beautifulsoup4 用法详解: beautifulsoup支持的一些解析库 解析器 使用方法 优势 劣势 Python标准库 Beautifu
BeautifulSoup是灵活又方便的网页解析库,处理高效,支持多种解析器 利用它不用编写正则表达式即可方便地实现网页信息的提取 安装:pip3 install beautifulsoup4 用法详解: beautifulsoup支持的一些解析库
基本使用方法: import bs4 from bs4 import BeautifulSoup #下面是一段不完整的 html代码 html = ‘‘‘ <html><head><title>The Demouse‘s story</title></head> <body> <p class="title" name="dromouse"><b>The Domouse‘s story</b></p> <p class="story">Once upon a time there were three little sisters,and their name were <a href="http://examlpe.com/elele" class="sister" ld="link1"><!--Elsle--></a> <a href="http://examlpe.com/lacie" class="sister" ld="link2"><!--Elsle--></a> <a href="http://examlpe.com/title" class="sister" ld="link3"><title></a> and they lived the bottom of a wall</p> <p clas="stuy">..</p> ‘‘‘ soup = BeautifulSoup(html,‘lxml‘) #将代码补全,也就是容错处理 print(soup.prettify()) #选择title这个标签,并打印内容 输出结果为: <html> <head> <title> The Demouse‘s story </title> </head> <body> <p class="title" name="dromouse"> <b> The Domouse‘s story </b> </p> <p class="story"> Once upon a time there were three little sisters,and their name were <a class="sister" href="http://examlpe.com/elele" ld="link1"> <!--Elsle--> </a> <a class="sister" href="http://examlpe.com/lacie" ld="link2"> <!--Elsle--> </a> <a class="sister" href="http://examlpe.com/title" ld="link3"> <title> </title> </a> and they lived the bottom of a wall </p> <p clas="stuy"> .. </p> </body> </html> The Demouse‘s story 标签选择器: 选择元素 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |