python爬虫xpath提取数据_python爬虫的页面数据解析和提取/xpath/bs4/jsonpath/正则(1)...

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

"""

#创建 Beautiful Soup 对象

soup =BeautifulSoup(html)#打开本地 HTML 文件的方式来创建对象#soup = BeautifulSoup(open('index.html'))

#格式化输出 soup 对象的内容

print soup.prettify()

输出结果:

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters;andtheir names were

,Lacie

and

Tillie;andthey lived at the bottom of a well.

...

如果我们在IPython2下执行,会看到这样一段警告:

9069330.html

1388706-20180611225248369-495554642.png

意思是,如果我们没有显式地指定解析器,所以默认使用这个系统的最佳可用HTML解析器(“LXML”)。如果你在另一个系统中运行这段代码,或者在不同的虚拟环境中,使用不同的解析器造成行为不同。

但是可以我们通过soup = BeautifulSoup(html,“lxml”)方式指定LXML解析器。

四.bs4的四大对象种类

Beautiful Soup将复杂的HTML文档转换成一个复杂的树形结构,每个节点都是Python对象,所有对象可以归纳为4种:

标签

NavigableString

BeautifulSoup

评论

1.标签

Tag通俗点讲就是HTML中的一个个标签,例如:

The Dormouse's story

The Dormouse's story

上面的等等title head a pHTML标签加上里面包括的内容就是Tag,那么试着使用Beautiful Soup来获取标签:

from bs4 import BeautifulSoup

html = """

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

"""

#创建 Beautiful Soup 对象

soup = BeautifulSoup(html)

print soup.title

#

The Dormouse's story

print soup.head

#

The Dormouse's story

print soup.a

#

print soup.p

#

The Dormouse's story

print type(soup.p)

#

我们可以利用汤加标签名轻松地获取这些标签的内容,但这些对象的类型是bs4.element.Tag。但是注意,它查找的是在所有内容中的第一个符合要求的标签。如果要查询所有的标签,后面会进行介绍。

对于Tag,它有两个重要的属性,是名和attrs

print soup.name

# [document] #soup 对象本身比较特殊,它的 name 即为 [document]

print soup.head.name

# head #对于其他内部标签,输出的值便为标签本身的名称

print soup.p.attrs

# {'class': ['title'], 'name': 'dromouse'}

# 在这里,我们把 p 标签的所有属性打印输出了出来,得到的类型是一个字典。

print soup.p['class'] # soup.p.get('class')

# ['title'] #还可以利用get方法,传入属性的名称,二者是等价的

soup.p['class'] = "newClass"

print soup.p # 可以对这些属性和内容等等进行修改

#

The Dormouse's story

del soup.p['class'] # 还可以对这个属性进行删除

print soup.p

#

The Dormouse's story

2. NavigableString

既然我们已经得到了标签的内容,那么问题来了,我们要想获取标签内部的文字怎么办呢?很简单,用.string即可,例如

print soup.p.string

# The Dormouse's story

print type(soup.p.string)

# In [13]:

3. BeautifulSoup

BeautifulSoup对象表示的是一个文档的内容。大部分时候,可以把它当作Tag对象,是一个特殊的Tag,我们可以分别获取它的类型,名称,以及属性来感受一下

print type(soup.name)

#

print soup.name

# [document]

print soup.attrs # 文档本身的属性为空

# {}

4.评论

注释对象是一个特殊类型的NavigableString对象,其输出的内容不包括注释符号。

print soup.a

#

print soup.a.string

# Elsie

print type(soup.a.string)

#

a标签里的内容实际上是注释,但是如果我们利用.string来输出它的内容时,注释符号已经去掉了。

五.遍历文档树

1.直接子节点:.contents .children属性

。内容

tag的.content属性可以将标签的子节点以列表的方式输出

print soup.head.contents

#[

The Dormouse's story]

输出方式为列表,我们可以用列表索引来获取它的某一个元素

print soup.head.contents[0]

#

The Dormouse's story

。孩子

它返回的不是一个列表,不过我们可以通过遍历获取所有子节点。

我们打印输出.children看一下,可以发现它是一个list生成器对象

print soup.head.children

#

for child in soup.body.children:

print child

结果:

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

2.所有子孙节点:.descendants属性

.contents和.children属性仅包含标签的直接子节点,.descendants属性可以对所有标签的子孙节点进行递归循环,和儿童类似,我们也需要遍历获取其中的内容。

for child in soup.descendants:

print child

运行结果:

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

The Dormouse's story

The Dormouse's story

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值