本文从WordPress迁移而来, 查看全部WordPress迁移文章
学习内容来自文档,主要将其内容翻译一下和做个简单的笔记
- 构造bs对象
1 | bs = bs4.BeautifulSoup(html) #传入一段html文本即可 |
- tag对象,可以直接通过bs对象.标签名得到
1 | head = bs.html.head #通过树形结构,索引到html标签下面的head标签及其包含的内容 |
- tag对象,标签名
1 | # <meta></meta> |
- tag对象,操作属性
1 | print tag['class'] #[]得到class属性的值,unicode对象 |
- tag对象,返回string
1 | # <a>this is a string</a> |
- tag对象,返回strings
1 | strings = head.strings |
- tag对象,contents
1 | tagList = html.contents |
- tag对象,输出
1 | tag.prettify() #以非常标准的格式输出,返回的是unicode对象 |