python BeautifulSoup使用方法详解

php中文网
发布: 2016-06-06 11:28:21
原创
1658人浏览过

直接看例子:

代码如下:


#!/usr/bin/python
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
html_doc = """

The Dormouse's story

The Dormouse's story


Once upon a time there were three little sisters; and their names were
Elsie,
Lacie and
Tillie;
and they lived at the bottom of a well.


...


"""
soup = BeautifulSoup(html_doc)
print soup.title
print soup.title.name
print soup.title.string
print soup.p
print soup.a
print soup.find_all('a')
print soup.find(id='link3')
print soup.get_text()

结果为:

代码如下:


The Dormouse's story
title
The Dormouse's story

The Dormouse's story


Elsie
[Elsie, Lacie, Tillie]
Tillie
The Dormouse's story
The Dormouse's story
Once upon a time there were three little sisters; and their names were
Elsie,
Lacie and
Tillie;
and they lived at the bottom of a well.
...

可以看出:soup 就是BeautifulSoup处理格式化后的字符串,soup.title 得到的是title标签,soup.p  得到的是文档中的第一个p标签,要想得到所有标签,得用find_all
函数。find_all 函数返回的是一个序列,可以对它进行循环,依次得到想到的东西.
get_text() 是返回文本,这个对每一个BeautifulSoup处理后的对象得到的标签都是生效的。你可以试试 print soup.p.get_text()
其实是可以获得标签的其他属性的,比如我要获得a标签的href属性的值,可以使用 print soup.a['href'],类似的其他属性,比如class也是可以这么得到的(soup.a['class'])。
特别的,一些特殊的标签,比如head标签,是可以通过soup.head 得到,其实前面也已经说了。
如何获得标签的内容数组?使用contents 属性就可以 比如使用 print soup.head.contents,就获得了head下的所有子孩子,以列表的形式返回结果,
可以使用 [num]  的形式获得 ,获得标签,使用.name 就可以。
获取标签的孩子,也可以使用children,但是不能print soup.head.children 没有返回列表,返回的是 ,
不过使用list可以将其转化为列表。当然可以使用for 语句遍历里面的孩子。
关于string属性,如果超过一个标签的话,那么就会返回None,否则就返回具体的字符串print soup.title.string 就返回了 The Dormouse's story
超过一个标签的话,可以试用strings
向上查找可以用parent函数,如果查找所有的,那么可以使用parents函数
查找下一个兄弟使用next_sibling,查找上一个兄弟节点使用previous_sibling,如果是查找所有的,那么在对应的函数后面加s就可以

如何遍历树?

使用find_all 函数

代码如下:


find_all(name, attrs, recursive, text, limit, **kwargs)

举例说明:

代码如下:


print soup.find_all('title')
print soup.find_all('p','title')
print soup.find_all('a')
print soup.find_all(id="link2")
print soup.find_all(id=True)

返回值为:

代码如下:


[

The Dormouse's story]
[

The Dormouse's story

]
[Elsie, Lacie, Tillie]
[Lacie]
[Elsie, Lacie, Tillie]

通过css查找,直接上例子:

95Shop仿醉品商城
95Shop仿醉品商城

95Shop可以免费下载使用,是一款仿醉品商城网店系统,内置SEO优化,具有模块丰富、管理简洁直观,操作易用等特点,系统功能完整,运行速度较快,采用ASP.NET(C#)技术开发,配合SQL Serve2000数据库存储数据,运行环境为微软ASP.NET 2.0。95Shop官方网站定期开发新功能和维护升级。可以放心使用! 安装运行方法 1、下载软件压缩包; 2、将下载的软件压缩包解压缩,得到we

95Shop仿醉品商城 0
查看详情 95Shop仿醉品商城

代码如下:


print soup.find_all("a", class_="sister")
print soup.select("p.title")

通过属性进行查找

代码如下:


print soup.find_all("a", attrs={"class": "sister"})

通过文本进行查找

代码如下:


print soup.find_all(text="Elsie")
print soup.find_all(text=["Tillie", "Elsie", "Lacie"])

限制结果个数

代码如下:


print soup.find_all("a", limit=2)

结果为:

代码如下:


[Elsie, Lacie, Tillie]
[

The Dormouse's story

]
[Elsie, Lacie, Tillie]
[u'Elsie']
[u'Elsie', u'Lacie', u'Tillie']
[Elsie, Lacie]

总之,通过这些函数可以查找到想要的东西。

相关标签:
python速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号