python:beautifulSoup学习(来自学习资料)

来源:互联网 发布:java往word中写入文本 编辑:程序博客网 时间:2024/06/06 03:25

罗列问题:
1、beautifulSoup是什么
是一个能够快速从html或者xml文档中获取数据的类库。
官方的说是,能够帮你节省一天或者几天的开发时间。
2、beautifulSoup如何安装
Pip install beautifulSoup4(如果是Python3.x就是用这个)
旧版本:beautifulSoup3
Pip install beautifulSoup
3、beautifulSoup如何使用
本文中只讲解通过beautifulSoup select选择器获取数据
还有很多其他用法,需要单独学习。

Html基础回顾:
这里写图片描述
标签:紧跟着尖括号的内容
类:class
属性:标签中的字段
编号:id

html_doc = """<html><head><title>The Dormouse's story</title></head><body><p class="title"><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>"""

beautifulSoup.select的几种查找数据的方式:

Soup.select(“title”) //通过标签查找Soup.select(“.sister”) //通过class查找Soup.select(“#link2”)//通过id查找Soup.select(“a[href]”) //判断a标签是否有href属性

在python控制台中使用beautifulSoup的部首
1、导入from bs4 import BeautifulSoup
2、创建一个BeautifulSoup的实例

通过标签查找:
这里写图片描述

通过class查找
这里写图片描述

Select返回的值是一个list,需要通过角标获取数据。

通过id查找
这里写图片描述
多个id使用逗号分隔

通过查找

方式一:
属性固定的值
这里写图片描述

方式二:
是否包含属性
这里写图片描述

原创粉丝点击