pythonbs4函数,pythonbs4模块

python中bs4.beautiful解析有时候不加前边的bs4 有什么区别?

这个就是看你是如何import包的

创新互联公司专注于三沙企业网站建设,响应式网站开发,商城网站制作。三沙网站建设公司,为三沙等地区提供建站服务。全流程定制开发,专业设计,全程项目跟踪,创新互联公司专业和态度为您提供的服务

如果是import bs4,那么就应该是bs4.BeautifulSoup

如果是from bs4 import BeautifulSoup,就可以直接使用BeautifulSoup

python bs4 的使用

html_doc = """

html

head

titleThe Dormouse's story/title

/head

body

p class="title aq"

b

The Dormouse's story

/b

/p

p class="story"Once upon a time there were three little sisters; and their names were

a href="" class="sister" id="link1"Elsie/a,

a href="" class="sister" id="link2"Lacie/a

and

a href="" class="sister" id="link3"Tillie/a;

and they lived at the bottom of a well.

./p

p class="story".../p

"""

html字符串创建BeautifulSoup对象 :

soup = BeautifulSoup(html_doc, 'html.parser', from_encoding='utf-8')

输出第一个 title 标签 :

print soup.title

输出第一个 title 标签的标签名称 :

print soup.title.name

输出第一个 title 标签的包含内容 :

print soup.title.string

输出第一个 title 标签的父标签的标签名称 :

print soup.title.parent.name

输出第一个 p 标签

print soup.p

输出第一个 p 标签的 class 属性内容 :

print soup.p['class']

输出第一个 a 标签的 href 属性内容 :

print soup.a['href']

'''''

soup的属性可以被添加,删除或修改. 再说一次, soup的属性操作方法与字典一样

'''

修改第一个 a 标签的href属性为

soup.a['href'] = ' '

给第一个 a 标签添加 name 属性 :

soup.a['name'] = u'百度'

删除第一个 a 标签的 class 属性为 :

del soup.a['class']

输出第一个 p 标签的所有子节点 :

print soup.p.contents

输出第一个 a 标签 :

print soup.a

输出所有的 a 标签,以列表形式显示 :

print soup.find_all('a')

输出第一个 id 属性等于 link3 的 a 标签 :

print soup.find(id="link3")

获取所有文字内容 :

print(soup.get_text())

输出第一个 a 标签的所有属性信息 :

print soup.a.attrs

for link in soup.find_all('a'):

获取 link 的 href 属性内容

print(link.get('href'))

对soup.p的子节点进行循环输出 :

for child in soup.p.children:

print(child)

正则匹配,名字中带有b的标签 :

for tag in soup.find_all(re.compile("b")):

print(tag.name)

import bs4#导入BeautifulSoup库

Soup = BeautifulSoup(html)#其中html 可以是字符串,也可以是句柄

需要注意的是,BeautifulSoup会自动检测传入文件的编码格式,然后转化为Unicode格式

通过如上两句话,BS自动把文档生成为如上图中的解析树。

python2.5可以调用bs4吗

一般情况下都是通过import脚本,然后直接调用脚本里的函数,调用函数就可以直接传递参数;因为Python并不像C语言那样有main函数。 import B(脚本名称)B.hello(参数A,参数B)

文章题目:pythonbs4函数,pythonbs4模块
转载来源:https://www.cdcxhl.com/article0/hcipoo.html

成都网站建设公司_创新互联,为您提供云服务器品牌网站设计标签优化网站内链手机网站建设网站设计公司

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

小程序开发