设为首页 加入收藏

TOP

爬虫——BeautifulSoup4解析器(六)
2017-09-30 17:57:09 】 浏览:1258
Tags:爬虫 BeautifulSoup4 解析
lt;p class="story">Once upon a time there were three little sisters; and their names were <a href="http://example.com/elsie" class="sister" id="link1"><!-- Elsie --></a>, <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p> """ # 创建 Beautiful Soup 对象,指定lxml解析器 soup = BeautifulSoup(html, "lxml") # 输出方式为列表生成器对象 print(soup.head.descendants) # 通过遍历获取所有子孙节点 for child in soup.head.descendants: print(child)

运行结果

<generator object descendants at 0x00519AB0>
<title>The Dormouse's story</title>
The Dormouse's story

3.节点内容:.string属性

如果Tag只有一个NavigableString类型子节点,那么这个Tag可以使用.string得到子节点。如果一个Tag仅有一个子节点,那么这个Tab也可以使用.string方法,输出结果与当前唯一子节点的.string结果相同。

通俗点来讲就是:如果一个标签里面没有标签了,那么.string就会返回标签里面的内容。如果标签里面只有唯一的一个标签了,那么.string也会返回里面的内容。例如:

#!/usr/bin/python3
# -*- coding:utf-8 -*-
__author__ = 'mayi'

from bs4 import BeautifulSoup

html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><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="http://example.com/elsie" class="sister" id="link1"><!-- Elsie --></a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""

# 创建 Beautiful Soup 对象,指定lxml解析器
soup = BeautifulSoup(html, "lxml")

print(soup.head.string)

print(soup.head.title.string)

运行结果

The Dormouse's story
The Dormouse's story

搜索文档树

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

1)name参数

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

a.传字符串

最简单的过滤器就是字符串,在搜索方法中传入一个字符串参数,Beautiful Soup会查找与字符串完整匹配所有的内容,返回一个列表。

#!/usr/bin/python3
# -*- coding:utf-8 -*-
__author__ = 'mayi'

from bs4 import BeautifulSoup

html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><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="http://example.com/elsie" class="sister" id="link1"><!-- Elsie --></a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3&qu
首页 上一页 3 4 5 6 7 8 9 下一页 尾页 6/10/10
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【人生苦短 PYTHON当歌】——PYTH.. 下一篇day5模块学习 -- os模块学习

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目