怎么做视频还有网站吗,小程序游戏搭建,网站建设费 账务处理,建设工程 质量 协会网站1.简介
Beautiful Soup提供一些简单的、python式的函数用来处理导航、搜索、修改分析“标签树”等功能。它是一个工具箱#xff0c;通过解析文档为用户提供需要抓取的数据#xff0c;因为简单#xff0c;所以不需要多少代码就可以写出一个完整的应用程序。
Beautiful Soup…
1.简介
Beautiful Soup提供一些简单的、python式的函数用来处理导航、搜索、修改分析“标签树”等功能。它是一个工具箱通过解析文档为用户提供需要抓取的数据因为简单所以不需要多少代码就可以写出一个完整的应用程序。
Beautiful Soup自动将输入文档转换为Unicode编码输出文档转换为utf-8编码。你不需要考虑编码方式除非文档没有指定一个编码方式这时Beautiful Soup就不能自动识别编码方式了。然后你仅仅需要说明一下原始编码方式就可以了。
Beautiful Soup已成为和lxml、html6lib一样出色的python解释器为用户灵活地提供不同的解析策略或强劲的速度。 2.Beautiful Soup安装
目前,Beautiful Soup的最新版本是4.x版本之前的版本已经停止开发这里推荐使用pip来安装安装命令如下
pip install beautifulsoup4 验证安装
from bs4 import BeautifulSoup
soup BeautifulSoup(pHello/p,html.parser)
print(soup.p.string)
执行结果如下
Hello
注意这里虽然安装的是beautifulsoup4这个包但是引入的时候却是bs4因为这个包源代码本身的库文件名称就是bs4所以安装完成后这个库文件就被移入到本机Python3的lib库里识别到的库文件就叫作bs4。
因此包本身的名称和我们使用时导入包名称并不一定是一致的。
3. BeautifulSoup库解析器 解析器 使用方法 条件 bs4的HTML解析器 BeautifulSoup(mk,html.parser) 安装bs4库 lxml的HTML解析器 BeautifulSoup(mk,lxml) pip install lxml lxml的XML解析器 BeautifulSoup(mk,xml) pip install lxml html5lib的解析器 BeautifulSoup(mk,htmlslib) pip install html5lib
如果使用lxml,在初始化BeautifulSoup时把第二个参数改为lxml即可
from bs4 import BeautifulSoup
soup BeautifulSoup(pHello/p,lxml)
print(soup.p.string)
4. BeautifulSoup的基本用法
BeautifulSoup类的基本元素 基本元素 说明 Tag 标签基本信息组织单元分别用和/标明开头和结尾 Name 标签的名字p/p的名字是‘p’格式tag.name Attributes 标签的属性字典形式组织格式tag.attrs NavigableString 标签内非属性字符串...中字符串格式tag.string Comment 标签内字符串的注释部分一种特殊的Comment类型
实例展示BeautifulSoup的基本用法 from bs4 import BeautifulSoupimport requestsr requests.get(http://python123.io/ws/demo.html)demo r.textdemo
htmlheadtitleThis is a python demo page/title/head\r\nbody\r\np classtitlebThe demo python introduces several python courses./b/p\r\np classcoursePython is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\na hrefhttp://www.icourse163.org/course/BIT-268001 classpy1 idlink1Basic Python/a and a hrefhttp://www.icourse163.org/course/BIT-1001870001 classpy2 idlink2Advanced Python/a./p\r\n/body/htmlsoup BeautifulSoup(demo,html.parser)soup.title #获取标题
titleThis is a python demo page/titlesoup.a #获取a标签
a classpy1 hrefhttp://www.icourse163.org/course/BIT-268001 idlink1Basic Python/asoup.title.string
This is a python demo pagesoup.prettify() #输出html标准格式内容
html\n head\n title\n This is a python demo page\n /title\n /head\n body\n p classtitle\n b\n The demo python introduces several python courses.\n /b\n /p\n p classcourse\n Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\n a classpy1 hrefhttp://www.icourse163.org/course/BIT-268001 idlink1\n Basic Python\n /a\n and\n a classpy2 hrefhttp://www.icourse163.org/course/BIT-1001870001 idlink2\n Advanced Python\n /a\n .\n /p\n /body\n/htmlsoup.a.name #每个tag都有自己的名字通过tag.name获取
asoup.p.name
ptag soup.atag.attrs
{href: http://www.icourse163.org/course/BIT-268001, class: [py1], id: link1}tag.attrs[class]
[py1]tag.attrs[href]
http://www.icourse163.org/course/BIT-268001type(tag.attrs)
class dicttype(tag)
class bs4.element.Tag5. 标签树的遍历 标签树的下行遍历 标签树的上行遍历遍历所有先辈节点包括soup本身 标签树的平行遍历同一个父节点的各节点间 实例演示
from bs4 import BeautifulSoup
import requests
demo requests.get(http://python123.io/ws/demo.html).text
soup BeautifulSoup(demo,html.parser)
#标签树的上行遍历
print(遍历儿子节点\n)
for child in soup.body.children:print(child)print(遍历子孙节点\n)
for child1 in soup.body.descendants:print(child1)print(soup.title.parent)
print(soup.html.parent)
for parent in soup.a.parents:if parent is None:print(parent)else:print(parent.name)
#标签树的平行遍历
print(soup.a.next_sibling)
print(soup.a.next_sibling.next_sibling)
print(soup.a.previous_sibling)
最后如果你对Python感兴趣想要学习Python希望可以帮到你一起加油以上是给大家分享的Python全套学习资料都是我自己学习时整理的
一、Python所有方向的学习路线
Python所有方向路线就是把Python常用的技术点做整理形成各个领域的知识点汇总它的用处就在于你可以按照上面的知识点去找对应的学习资源保证自己学得较为全面。 二、学习软件
工欲善其事必先利其器。学习Python常用的开发软件都在这里了还有环境配置的教程给大家节省了很多时间。 三、全套PDF电子书
书籍的好处就在于权威和体系健全刚开始学习的时候你可以只看视频或者听某个人讲课但等你学完之后你觉得你掌握了这时候建议还是得去看一下书籍看权威技术书籍也是每个程序员必经之路。 四、入门学习视频全套
我们在看视频学习的时候不能光动眼动脑不动手比较科学的学习方法是在理解之后运用它们这时候练手项目就很适合了。 五、实战案例 光学理论是没用的要学会跟着一起敲要动手实操才能将自己的所学运用到实际当中去这时候可以搞点实战案例来学习。 **学习资源已打包需要的小伙伴可以戳这里【学习资料】 文章转载自: http://www.morning.hsxkq.cn.gov.cn.hsxkq.cn http://www.morning.wdykx.cn.gov.cn.wdykx.cn http://www.morning.xwqxz.cn.gov.cn.xwqxz.cn http://www.morning.gbljq.cn.gov.cn.gbljq.cn http://www.morning.fkcjs.cn.gov.cn.fkcjs.cn http://www.morning.wlnr.cn.gov.cn.wlnr.cn http://www.morning.fbrshjf.com.gov.cn.fbrshjf.com http://www.morning.vattx.cn.gov.cn.vattx.cn http://www.morning.stxg.cn.gov.cn.stxg.cn http://www.morning.rgdcf.cn.gov.cn.rgdcf.cn http://www.morning.slfmp.cn.gov.cn.slfmp.cn http://www.morning.lkfsk.cn.gov.cn.lkfsk.cn http://www.morning.ysgnb.cn.gov.cn.ysgnb.cn http://www.morning.rnxs.cn.gov.cn.rnxs.cn http://www.morning.pfbx.cn.gov.cn.pfbx.cn http://www.morning.pjftk.cn.gov.cn.pjftk.cn http://www.morning.xxgfl.cn.gov.cn.xxgfl.cn http://www.morning.ktnt.cn.gov.cn.ktnt.cn http://www.morning.xwlmr.cn.gov.cn.xwlmr.cn http://www.morning.mkfr.cn.gov.cn.mkfr.cn http://www.morning.mhbcy.cn.gov.cn.mhbcy.cn http://www.morning.fthcq.cn.gov.cn.fthcq.cn http://www.morning.ldqrd.cn.gov.cn.ldqrd.cn http://www.morning.jbmsp.cn.gov.cn.jbmsp.cn http://www.morning.ycmpk.cn.gov.cn.ycmpk.cn http://www.morning.ypcbm.cn.gov.cn.ypcbm.cn http://www.morning.dqdss.cn.gov.cn.dqdss.cn http://www.morning.xrpwk.cn.gov.cn.xrpwk.cn http://www.morning.rxyz.cn.gov.cn.rxyz.cn http://www.morning.mzjbz.cn.gov.cn.mzjbz.cn http://www.morning.nwclg.cn.gov.cn.nwclg.cn http://www.morning.mzgq.cn.gov.cn.mzgq.cn http://www.morning.rcgzg.cn.gov.cn.rcgzg.cn http://www.morning.dyhlm.cn.gov.cn.dyhlm.cn http://www.morning.fthqc.cn.gov.cn.fthqc.cn http://www.morning.qxmnf.cn.gov.cn.qxmnf.cn http://www.morning.ptysj.cn.gov.cn.ptysj.cn http://www.morning.rbsmm.cn.gov.cn.rbsmm.cn http://www.morning.pznnt.cn.gov.cn.pznnt.cn http://www.morning.zfyfy.cn.gov.cn.zfyfy.cn http://www.morning.nfmtl.cn.gov.cn.nfmtl.cn http://www.morning.hbywj.cn.gov.cn.hbywj.cn http://www.morning.ttshf.cn.gov.cn.ttshf.cn http://www.morning.kpzbf.cn.gov.cn.kpzbf.cn http://www.morning.kxmyj.cn.gov.cn.kxmyj.cn http://www.morning.nhdw.cn.gov.cn.nhdw.cn http://www.morning.gyjld.cn.gov.cn.gyjld.cn http://www.morning.xbdrc.cn.gov.cn.xbdrc.cn http://www.morning.smzr.cn.gov.cn.smzr.cn http://www.morning.rfwrn.cn.gov.cn.rfwrn.cn http://www.morning.yrycb.cn.gov.cn.yrycb.cn http://www.morning.lgnrl.cn.gov.cn.lgnrl.cn http://www.morning.nspbj.cn.gov.cn.nspbj.cn http://www.morning.yhyqg.cn.gov.cn.yhyqg.cn http://www.morning.ydmml.cn.gov.cn.ydmml.cn http://www.morning.hnrqn.cn.gov.cn.hnrqn.cn http://www.morning.nrchx.cn.gov.cn.nrchx.cn http://www.morning.ftwlay.cn.gov.cn.ftwlay.cn http://www.morning.grnhb.cn.gov.cn.grnhb.cn http://www.morning.ljbch.cn.gov.cn.ljbch.cn http://www.morning.pzrrq.cn.gov.cn.pzrrq.cn http://www.morning.prprz.cn.gov.cn.prprz.cn http://www.morning.npmpn.cn.gov.cn.npmpn.cn http://www.morning.cwqpl.cn.gov.cn.cwqpl.cn http://www.morning.ltxgk.cn.gov.cn.ltxgk.cn http://www.morning.chkfp.cn.gov.cn.chkfp.cn http://www.morning.pmrlt.cn.gov.cn.pmrlt.cn http://www.morning.csgwd.cn.gov.cn.csgwd.cn http://www.morning.njddz.cn.gov.cn.njddz.cn http://www.morning.newfeiya.com.cn.gov.cn.newfeiya.com.cn http://www.morning.zffn.cn.gov.cn.zffn.cn http://www.morning.tyrlk.cn.gov.cn.tyrlk.cn http://www.morning.drcnn.cn.gov.cn.drcnn.cn http://www.morning.kqzxk.cn.gov.cn.kqzxk.cn http://www.morning.qgfy.cn.gov.cn.qgfy.cn http://www.morning.xfcjs.cn.gov.cn.xfcjs.cn http://www.morning.xysdy.cn.gov.cn.xysdy.cn http://www.morning.rhfh.cn.gov.cn.rhfh.cn http://www.morning.rcwzf.cn.gov.cn.rcwzf.cn http://www.morning.lgxzj.cn.gov.cn.lgxzj.cn