python爬虫笔记day1 BS4库的使用
来源:互联网 发布:淘宝全球购上传身份证 编辑:程序博客网 时间:2024/05/17 05:51
bs4库的简单使用
这里我们先简单的讲解一下bs4库的使用,
暂时不去考虑如何从web上抓取网页,
假设我们需要爬去的html是如下这么一段:
下面的一段HTML代码将作为例子被多次用到.这是 爱丽丝梦游仙境的 的一段内容(以后内容中简称为 爱丽丝 的文档):
`html<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 werehttp://example.com/elsie" class="sister" id="link1">Elsie,http://example.com/lacie" class="sister" id="link2">Lacie andhttp://example.com/tillie" class="sister" id="link3">Tillie;and they lived at the bottom of a well.</p><p class="story">...</p></html>
- 下面我们开始用bs4库解析这一段html网页代码。
#导入bs4模块from bs4 import BeautifulSoup#做一个美味汤soup = BeautifulSoup(html,'html.parser')#输出结果print(soup.prettify())'''OUT:# <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 class="sister" href="http://example.com/elsie" id="link1"># Elsie# </a># ,# <a class="sister" href="http://example.com/lacie" id="link2"># Lacie# </a># and# <a class="sister" href="http://example.com/tillie" id="link2"># Tillie# </a># ; and they lived at the bottom of a well.# </p># <p class="story"># ...# </p># </body># </html>'''
可以看到bs4库将网页文件变成了一个soup的类型,
事实上,bs4库 是解析、遍历、维护、“标签树“的功能库。
通俗一点说就是: bs4库把html源代码重新进行了格式化,
从而方便我们对其中的节点、标签、属性等进行操作。
- 下面是几个简单的浏览结构化数据的方式 :
请仔细观察最前面的html文件
#找到文档的titlesoup.title# <title>The Dormouse's story</title>#title的name值soup.title.name# u'title'#title中的字符串Stringsoup.title.string# u'The Dormouse's story'#title的父亲节点的name属性soup.title.parent.name# u'head'#文档的第一个找到的段落soup.p# <p class="title"><b>The Dormouse's story</b></p>#找到的p的class属性值soup.p['class']# u'title'#找到a标签soup.a# http://example.com/elsie" id="link1">Elsie#找到所有的a标签soup.find_all('a')# [http://example.com/elsie" id="link1">Elsie,# http://example.com/lacie" id="link2">Lacie,# http://example.com/tillie" id="link3">Tillie]#找到id值等于3的a标签soup.find(id="link3")# http://example.com/tillie" id="link3">Tillie
通过上面的例子 我们知道bs4库是这样理解一个html源文件的:
- 首先 把html源文件转换为soup类型
- 接着 从中通过特定的方式抓取内容
更高级点的用法?
- 从文档中找到所有<a>标签的链接:
#发现了没有,find_all方法返回的是一个可以迭代的列表for link in soup.find_all('a'): print(link.get('href')) # http://example.com/elsie # http://example.com/lacie # http://example.com/tillie
- 从文档中获取所有文字内容:
#我们可以通过get_text 方法 快速得到源文件中的所有text内容。print(soup.get_text())# 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.## ...
bs4库的入门使用我们就先进行到这。
阅读全文
0 0
- python爬虫笔记day1 BS4库的使用
- Python爬虫之bs4库
- 从零开始写Python爬虫 --- 1.2 BS4库的安装与使用
- 从零开始写Python爬虫 --- 1.3 BS4库的解析器
- 爬虫笔记2(转)BS4库的解析器
- requests和bs4的python爬虫入门
- Python爬虫(bs4)-1
- Python爬虫(bs4)-2
- Python爬虫(bs4)-3
- Python -bs4反爬虫解决方法
- python爬虫由浅入深6--基于bs4库的HTML内容的查找方法
- python——爬虫学习——基于bs4库的HTML内容查找方法-(3)
- Python网络爬虫与信息提取-Day7-基于bs4库的HTML内容遍历方法
- Python网络爬虫与信息提取-Day8-基于bs4库的HTML格式输出
- Python学习笔记-1、Python3的urlib.request和bs4.BeautifulSoup使用
- day1的笔记(python)
- bs4库的安装与使用
- bs4的使用
- 【Leetcode-easy-463】Island Perimeter
- poj 3468 A Simple Problem with Integers
- MVC,MVP及MVVM
- “谜途”知返:从流水线式开发者到独立游戏人的暖心游戏
- 敲黑板,中秋节猜谜派奖时间到啦!
- python爬虫笔记day1 BS4库的使用
- 总结3
- “FCoE全解系列”之融合网络方案和典型组网
- Springboot实现filter拦截token验证和跨域
- Linux基础练习题(5)
- Android多媒体--MediaCodec 中文API文档
- 周总结
- STC15W系列MCU的串口1的简单设置
- Python网站推荐:Pythontutor(可视化代码执行过程)