Python网络爬虫(三)——Beutiful Soup库入门
来源:互联网 发布:13.3寸mac玩cf分辨率 编辑:程序博客网 时间:2024/06/18 11:22
Beautiful Soup库
Beautiful Soup库的理解
Beautiful Soup库是解析、遍历、维护“标签树”的功能库
Beautiful Soup库的引用
Beautiful Soup库,也叫beautifulsoup4 或bs4
约定引用方式如下,即主要是用BeautifulSoup类
from bs4 import BeautifulSoup import bs4
BeautifulSoup类
BeautifulSoup对应一个HTML/XML文档的全部内容
Beautiful Soup库解析器
soup = BeautifulSoup('<html>data<html>','html.parser')
BeautifulSoup类的基本元素
<p class=“title”> … </p>
Tag标签
>>> import requests >>> r = requests.get("http://python123.io/ws/demo.html") >>> r.text >>> demo = r.text >>> from bs4 import BeautifulSoup >>> soup = BeautifulSoup(demo,'html.parser') >>> print(soup.prettify()) >>> soup.title <title>This is a python demo page</title> >>> soup.a <a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>
任何存在于HTML语法中的标签都可以用soup.访问获得
当HTML文档中存在多个相同对应内容时,soup.返回第一个
Tag的name(名字)
>>> soup.a.name 'a' >>> soup.a.parent.name 'p'
每个都有自己的名字,通过.name获取,字符串类型
Tag的attrs(属性)
>>> tag = soup.a >>> tag.attrs {'href': 'http://www.icourse163.org/course/BIT-268001', 'class': ['py1'], 'id': 'link1'}
一个可以有0或多个属性,字典类型
Tag的NavigableString
>>> soup.a.string 'Basic Python' >>> soup.p.string 'The demo python introduces several python courses.' >>> type(soup.p.string)
NavigableString可以跨越多个层次
Tag的Comment
>>> newsoup = BeautifulSoup("<b><!--This is a comment--></b><p>This is not a comment</p>","html.parser") >>> newsoup.b.string 'This is a comment' >>> type(newsoup.b.string) <class 'bs4.element.Comment'> >>> newsoup.p.string 'This is not a comment' >>> type(newsoup.p.string) <class 'bs4.element.NavigableString'>
Comment是一种特殊类型
基于bs4库的HTML内容遍历方法
HTML基本格式
<html> <head> <title>This is a python demo page</title> </head> <body> <p class="title"> <b>The demo python introduces several python courses.</b> </p> <p class=“course”>Python is a wonderful general‐purpose programming language. You can learn Python from novice to professional by tracking the following courses: <a href="http://www.icourse163.org/course/BIT‐268001" class="py1" id="link1">Basic Python</a> and <a href="http://www.icourse163.org/course/BIT‐1001870001" class="py2" id="link2">Advanced Python</a>. </p> </body> </html>
<>…<\> (应为“/”)构成了所属关系,形成了标签的树形结构
标签树的下行遍历
BeautifulSoup类型是标签树的根节点
遍历儿子节点:
for child in soup.body.children: print(child)
遍历子孙节点:
for child in soup.body.descendants: print(child)
标签树的上行遍历
遍历所有先辈节点,包括soup本身,所以要区别判断:
>>> for parent in soup.a.parents: if parent is None: print(parent) else: print(parent.name) p body html [document]
标签树的平行遍历
遍历后续节点
for sibling in soup.a.next_sibling: print(sibling)
遍历前续节点
for sibling in soup.a.previous_sibling: print(sibling)
小结
基于bs4库的HTML格式输出
bs4库的prettify()方法
.prettify()为HTML文本<>及其内容增加更加’\n’
.prettify()可用于标签,方法:.prettify()
bs4库的编码
bs4库将任何HTML输入都变成utf‐8编码
Python 3.x默认支持编码是utf‐8,解析无障碍
阅读全文
0 0
- Python网络爬虫(三)——Beutiful Soup库入门
- Python网络爬虫——Beautiful Soup库
- python网络爬虫基础(2)--Beautiful Soup库
- python网络爬虫入门(三)———多线程
- Python爬虫学习二——Beautiful Soup库
- 网络爬虫系列笔记(3)——Beautiful Soup库
- Python网络爬虫与信息提取-Day6-Beautiful Soup库
- python爬虫入门--Beautiful Soup库介绍及实例
- Python爬虫入门(8):Beautiful Soup的用法
- Python爬虫入门(8):Beautiful Soup的用法
- Python爬虫入门(8):Beautiful Soup的用法
- Python爬虫入门(8):Beautiful Soup的用法
- python 网络爬虫 Beautiful Soup用法
- Python爬虫入门-Beautiful Soup的用法
- python网络爬虫入门(三)—— 做个简陋的pixabay 图片下载器
- Python 定向爬虫入门—Python(三)
- Python爬虫入门(三)BeautifulSoup库
- python网络爬虫(三)
- 增删改查
- h5 安卓input输入框和改变div 字体颜色
- Vue axios处理http请求
- DB2创建联邦数据库
- 消息中间件简介
- Python网络爬虫(三)——Beutiful Soup库入门
- 远程使用jupyter
- 正则表达式
- 在SpringBoot中使用JSP开发网页
- 测试用例设计(以QQ传输文件为例)
- db2cmd
- 每日一题之查找单链表的第K个节点
- Linux常用命令
- linux CAT与ECHO指令用法详解