Python 3爬虫网易云(二)—— BeautifulSoup库用法上篇
来源:互联网 发布:最优化计算方法 pdf 编辑:程序博客网 时间:2024/05/16 06:30
写在前面
上一篇演示了如何使用requests模块向网站发送http请求,获取到网页的HTML数据。这篇来演示如何使用BeautifulSoup模块来从HTML文本中提取我们想要的数据。
运行环境
我的运行环境如下:系统版本:Windows10。Python版本:Python3.6 IDE:Pycharm
BeautifulSoup ——解析html文档
首先来看BeautifulSoup的对象种类,在使用的过程中就会了解你获取到的东西接下来应该如何操作。
Beautiful Soup将复杂HTML文档转换成一个复杂的树形结构,每个节点都是Python对象。所有对象可以归纳为4种类型:Tag , NavigableString , BeautifulSoup , Comment 。下面我们分别看看这四种类型都是什么东西。
1>.Tag
这个就跟HTML或者XML(还能解析XML?是的,能!)中的标签是一样一样的。我们使用find()方法返回的类型就是这个(插一句:使用find-all()返回的是多个该对象的集合,是可以用for循环遍历的。)。返回标签之后,还可以对提取标签中的信息。
提取标签的名字:
tag.name
tag['attribute']
from bs4 import BeautifulSouphtml_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>"""soup = BeautifulSoup(html_doc, 'lxml') #声明BeautifulSoup对象find = soup.find('p') #使用find方法查到第一个p标签print("find's return type is ", type(find)) #输出返回值类型print("find's content is", find) #输出find获取的值print("find's Tag Name is ", find.name) #输出标签的名字print("find's Attribute(class) is ", find['class']) #输出标签的class属性值运行结果如下:
find's return type is <class 'bs4.element.Tag'>find's content is <p class="title"><b>The Dormouse's story</b></p>find's Tag Name is pfind's Attribute(class) is ['title']
2>.NavigableString
NavigableString就是标签中的文本内容(不包含标签)。获取方式如下:
还是以上面那个例子,加上下面这行,然后执行:
tag.string
还是以上面那个例子,加上下面这行,然后执行:
print('NavigableString is:', find.string)
NavigableString is: The Dormouse's story
3>.BeautifulSoup
BeautifulSoup对象表示一个文档的全部内容。支持遍历文档树和搜索文档树
4>.Comment
from bs4 import BeautifulSoupmarkup = "<b><!--Hey, buddy. Want to buy a used parser?--></b>"soup = BeautifulSoup(markup)comment = soup.b.stringprint(comment)print(type(comment))
结果如下
Hey, buddy. Want to buy a used parser?<class 'bs4.element.Comment'>
阅读全文
1 0
- Python 3爬虫网易云(二)—— BeautifulSoup库用法上篇
- Python 3爬虫网易云(三)—— BeautifulSoup库用法下篇
- Python 3爬虫网易云(四)—— 正则表达式上篇之匹配法则
- Python 3爬虫网易云(一)—— Requests库用法
- python爬虫系列(2)—— requests和BeautifulSoup库的基本用法
- Python 爬虫 —— BeautifulSoup
- Python从零开始写爬虫(二)BeautifulSoup库使用
- Python爬虫BeautifulSoup用法(1)
- Python 3爬虫网易云(八)—— 对网易云歌词的爬取
- 爬虫学习笔记(二)——BeautifulSoup
- Python 3爬虫网易云(六)—— 第三方库安装
- python爬虫(1)——BeautifulSoup库函数find_all()
- python爬虫——BeautifulSoup基础操作
- python爬虫——BeautifulSoup 抓取图片
- Python爬虫入门(三)BeautifulSoup库
- 【爬虫学习笔记】BeautifulSoup用法分析(二)
- Python网络爬虫与信息提取(二) BeautifulSoup库
- Python爬虫包 BeautifulSoup 学习(二) 异常处理
- 作为一个Java程序员,我在很苦逼地上班
- laravel框架使用mongodb数据库
- XMPP协议官网资料
- ELK安装配置
- try catch finally,try里有return,finally还执行么?
- Python 3爬虫网易云(二)—— BeautifulSoup库用法上篇
- Matlab取整函数
- 探秘Jps及无法查看java进程问题
- 虚函数与多态
- 正则表达式
- Android 面试总结 Handler内存泄漏分析及解决
- 卡西欧与美图的PK,不仅斗图被美图秒杀,最后连这个也不放过!
- JAVA泛型通配符T,E,K,V区别,T以及Class<T>,Class<?>的区别
- C#实现控制Windows系统关机、重启和注销的方法: