besutifulSoup用法

besutifulSoup是一种解析器。返回的是beautifulsoup类型的。

安装:pip install beautifulsoup4

引入:from bs4 import BeautifulSoup

初始化:Bs=beautifulSoup(html,‘html.parse’)python默认解析器,也可‘lxml’(更快,需安装

(安装lxml:pip install lxml)

格式化输出print bs.prettify() 输出的是html代码

选择器:

1.标签选择器(p,a,head,title,ul,li……)

Bs=beautifulSoup(html,‘html.parse’)python默认解析器

bs.a :整个a标签的html代码

Bs.a.string :a标签的全部内容,有注释的话,会自动去掉注释符号,留下注释文本

Bs.a.contents:  a标签的全部内容,tag 的 .content 属性可以将tag的子节点以列表的方式输出,可用列表索引来获取它的某一个元素

Bs.a.children:a标签的所有子节点,它返回的不是一个 list,不过我们可以通过遍历获取所有子节点

Bs.a.get_text(): 忽略注释的内容

bs.a.name:a标签的名字:a

bs.a.attrs :a标签所有属性

  bs.a['class']

  bs.a.get('class') 可修改:bs.a.get('class')=‘newclass’,可删除:del bs.a['class']

 2.find_all,find

重点介绍下find_all()方法:

find_all( name , attrs , recursive , text , **kwargs )
  • 1

(1)name参数

name参数可以查找所有名字为name的Tag,字符串对象自动忽略掉。

print bs.find_all('a')
  • 1

传列表:

print bs.find_all(['a','b'])
  • 1

传入正则表达式:

print bs.find_all(re.compile('^b'))
  • 1

所有以b开头的标签对象都会被找到。 
传递方法:

def has_class_but_not_id(tag):
    return tag.has_attr('class') and not tag.has_attr('id')
print bs.find_all(has_class_but_not_id)

(2)kwyowrds关键字

print bs.find_all(id='css')
print bs.find_all(id=re.compile('^a'))

还可以混合使用:

print bs.find_all(id='css',href=re.compile('^ex'))
  • 1

可以使用class作为过滤,但是class是Python中的关键字,可以使用class_代替,或者采用字典的形式传输参数:

print bs.find_all(class_='css')
print bs.find_all(attrs={'class':'css'})

(3)text参数

用来搜索文档中的字符串内容,text参数也接收字符串、正则表达式、列表、True等参数。

print bs.find_all(text=re.compile('^abc'))
  • 1

(4)limit参数

限制返回对象的个数,与数据库SQL查询类似。

(5)recursive参数

调用tag的find_all()方法时,BeautifulSoup会检索当前tag的所有子孙节点,如果只想搜索tag的直接子节点,可以使用参数 recursive=False。

3. CSS选择器

可以采用CSS的语法格式来筛选元素:

#标签选择器
print bs.select('a')
#类名选择器
print bs.select('.css')
#id选择器
print bs.select('#css')
#属性选择器
print bs.select('a[class="css"]')
#遍历
for tag in bs.select('a'):
    print tag.get_text()


参考:https://blog.csdn.net/kikaylee/article/details/56841789
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
com.bes.enterprise.appserv.deployment.exception.StartupFailedException: Exception occurred while starting the application. at com.bes.enterprise.appserv.deployment.manager.AppDeployManager$2.run(AppDeployManager.java:253) at com.bes.enterprise.appserv.deployment.manager.AppDeployManager.applyDeploymentContext(AppDeployManager.java:297) at com.bes.enterprise.appserv.deployment.manager.AppDeployManager.doLoad(AppDeployManager.java:245) at com.bes.enterprise.appserv.deployment.manager.ApplicationLifecycle.load(ApplicationLifecycle.java:77) at com.bes.enterprise.appserv.deployment.AppDeployer.load(AppDeployer.java:264) at com.bes.enterprise.appserv.deployment.handler.EnableApplicationHandler.doLoad(EnableApplicationHandler.java:133) at com.bes.enterprise.appserv.deployment.handler.EnableApplicationHandler.load(EnableApplicationHandler.java:71) at com.bes.enterprise.appserv.deployment.handler.EnableApplicationHandler.doApplicationProcess(EnableApplicationHandler.java:54) at com.bes.enterprise.appserv.deployment.handler.AbstractApplicationHandler$1.call(AbstractApplicationHandler.java:69) at com.bes.enterprise.appserv.deployment.handler.AbstractApplicationHandler$1.call(AbstractApplicationHandler.java:66) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:750) at com.bes.enterprise.appserv.deployment.handler.AbstractApplicationHandler$TerminableThread.run(AbstractApplicationHandler.java:177) Caused by: com.bes.enterprise.ejb.BESException: Creating application failed: /besweb/webapp/prpall: ContainerBase.addChild: start: com.bes.enterprise.webtier.LifecycleException: Failed to start component [WebEngine[com.bes.appserv].VirtualHost[server].DefaultContext[/prpall]] at com.bes.enterprise.ejb.assembler.classic.assemblercontext.ApplicationDeployer.doCreateApplication(ApplicationDeployer.java:458) at com.bes.enterprise.ejb.assembler.classic.assemblercontext.ApplicationDeployer.createApplication(ApplicationDeployer.java:342) at com.bes.enterprise.ejb.assembler.classic.assemblercontext.ApplicationDeployer.createApplication(ApplicationDeployer.java:331) at com.bes.enterprise.ejb.assembler.classic.Assembler.createApplication(Assembler.java:258) at com.bes.enterprise.appserv.deployment.manager.AppDeployManager$2.run(AppDeployManager.java:251) ... 14 more Caused by: java.lang.IllegalStateException: ContainerBase.addChild: start: com.bes.enterprise.webtier.LifecycleException: Failed to start component [WebEngine[com.bes.appserv].VirtualHost[server].DefaultContext[/prpall]] at com.bes.enterprise.webtier.core.ContainerBase.addChildInternal(ContainerBase.java:745) at com.bes.enterprise.webtier.core.ContainerBase.addChild(ContainerBase.java:717) at com.bes.enterprise.webtier.core.DefaultHost.addChild(DefaultHost.java:711) at com.bes.enterprise.webext.BESWebAppBuilder.deployWebApps(BESWebAppBuilder.java:511) at com.bes.enterprise.ejb.assembler.classic.assemblercontext.ApplicationDeployer.deployWebApps(ApplicationDeployer.java:1499) at com.bes.enterprise.ejb.assembler.classic.assemblercontext.ApplicationDeployer.doCreateApplication(ApplicationDeployer.java:437) ... 18 more
07-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值