Beautiful Soup 库安装展开目录
Win 平台:执行 pip install BeautifulSoup
Beautiful Soup 库使用展开目录
演示 HTML 页面地址:https://python123.io/ws/demo.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>
手工获取 demo.html 源代码,浏览器右键 “查看源代码”
- >>> demo = '''
- <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>'''
或者,用 Requests 库获取 demo.html 源代码
- >>> import requests
- >>> r = requests.get('https://python123.io/ws/demo.html')
- >>> demo = r.text
接下来使用 Beautiful Soup 库
- >>> from bs4 from BeautifulSoup
- >>> soup = BeautifulSoup(demo,'html.parser')
- >>> print(soup.prettify())
- <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 class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">
- Basic Python
- </a>
- and
- <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">
- Advanced Python
- </a>
- .
- </p>
- </body>
- </html>
Beautiful Soup 库的基本元素展开目录
Beautiful Soup 库的理解展开目录
Beautiful Soup 库是解析、遍历、维护 “标签树” 的功能库
Beautiful Soup 库的引用展开目录
Beautifule Soup 库,也叫 BeautifulSoup4 或 bs4,约定引用方式如下,即主要是用 BeautifulSoup 类
- from bs4 import BeautifulSoup
- import bs4
BeautifulSoup 类展开目录
BeautifulSoup 对应一个 HTML/XML 文档的全部内容
Beautiful Soup 库解析器展开目录
BeautifulSoup 类的基本元素展开目录
Tag 标签展开目录
基本元素 | 说明 |
---|---|
Tag | 标签,最基本的信息组织单元,分别用 <> 和 </> 标明开头和结尾 |
- >>> from bs4 import BeautifulSoup
- >>> soup = BeautifulSoup(demo,"html.parser")
- >>> soup.title
- <title>This is a python demo page</title>
- >>> tag = soup.a
- >>> tag
- <a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>
任何存在于 HTML 语法中的标签都可以用 soup.<tag> 访问获得 当 HTML 文档中存在多个相同 < tag > 对应内容时,soup.<tag > 返回第一个
Tag 的 name(名字)展开目录
基本元素 | 说明 |
---|---|
Name | 标签的名字,<p>…</p > 的名字是 'p',格式:<tag>.name |
- >>> from bs4 import BeautifulSoup
- >>> soup = BeautifulSoup(demo,"html.parser")
- >>> soup.a.name
- 'a'
- >>> soup.a.parent.name
- 'p'
- >>> soup.a.parent.parent.name
- 'body'
每个 <tag> 都有自己的名字,通过 < tag>.name 获取,字符串类型
Tag 的 attrs(属性)展开目录
基本元素 | 说明 |
---|---|
Attributes | 标签的属性,字典形式组织,格式:<tag>.attrs |
- >>> soup.a.attrs
- {'href': 'http://www.icourse163.org/course/BIT-268001', 'class': ['py1'], 'id': 'link1'}
- >>> kv = soup.a.attrs
- >>> kv['class']
- ['py1']
- >>> kv['href']
- 'http://www.icourse163.org/course/BIT-268001'
- >>> tyep(soup.a)
- <class 'bs4.element.Tag'>
- >>> type(soup.a.attrs)
- <class 'dict'>
一个 <tag> 可以有 0 或多个属性,字典类型
Tag 的 NavigableString展开目录
基本元素 | 说明 |
---|---|
NavigableString | 标签内非属性字符串,<>…</> 中字符串,格式:<tag>.string |
- >>> soup.a
- >>> soup.a.string
- 'Basic Python'
- >>> soup.p
- <p class="title"><b>The demo python introduces several python courses.</b></p>
- >>> soup.p.string
- 'The demo python introduces several python courses.'
- >>> type(soup.p.string)
- <class 'bs4.element.NavigableString'>
NavigableString 可以跨越多个层次
Tag 的 Comment展开目录
基本元素 | 说明 |
---|---|
Comment | 标签内字符串的注释部分,一种特殊的 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'
- >>> newsoup.p.string
- 'This is not a comment'
- >>> type(newsoup.b.string)
- <class 'bs4.element.Comment'>
- >>> type(newsoup.p.string)
- <class 'bs4.element.NavigableString'>
基于 bs4 库的 HTMl 内容遍历方法展开目录
标签树的下行遍历展开目录
- >>> soup = BeautifulSoup(demo,'html.parser')
- >>> soup.head
- <head><title>This is a python demo page</title></head>
- >>> print(soup.head.contents)
- [<title>This is a python demo page</title>]
- >>> print(soup.body.contents)
- ['\n', <p class="title"><b>The demo python introduces several python courses.</b></p>, '\n', <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 class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a> and <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>.</p>, '\n']
- >>> len(soup.body.contens)
- 5
- >>> soup.body.contens[1]
- <p class="title"><b>The demo python introduces several python courses.</b></p>
标签树的迭代下行遍历
标签树的上行遍历展开目录
- >>> soup = BeautifulSoup(demo,'html.parser')
- >>> soup.title.parent
- <head><title>This is a python demo page</title></head>
- >>> soup.html.parent
- <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 class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a> and <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>.</p>
- </body></html>
标签树的迭代上行遍历
- import requests
- from bs4 import BeautifulSoup
- r = requests.get("https://python123.io/ws/demo.html")
- demo = r.text
- soup = BeautifulSoup(demo,'html.parser')
- for parent in soup.a.parents:
- if parent is None:
- print(parent)
- else:
- print(parent.name)
遍历所有先辈节点,包括 soup 本身,所以要区别判断
标签树的平行遍历展开目录
平行遍历发生在同一个父节点下的各节点间
- >>> soup = BeautifulSoup(demo,'html.parser')
- >>> soup.a.next_sibling
- ' and '
- >>> soup.a.next_sibling.next_sibling
- <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>
- >>> soup.a.previous_sibling
- 'Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\n'
标签树的迭代平行遍历
标签树的遍历总结展开目录
bs4 库的 prettify () 方法展开目录
prettify()
方法的作用是使 HTML 内容更友好的展示
普通方法打印 HTML 文本
- >>> import requests
- >>> r = requests.get("https://python123.io/ws/demo.html")
- >>> r.text
- '<html><head><title>This is a python demo page</title></head>\r\n<body>\r\n<p class="title"><b>The demo python introduces several python courses.</b></p>\r\n<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\n<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>\r\n</body></html>'
使用 prettify()
方法
- >>> from bs4 import BeautifulSoup
- >>> soup = BeautifulSoup(r.text,'html.parser')
- >>> soup.prettify()
- '<html>\n <head>\n <title>\n This is a python demo page\n </title>\n </head>\n <body>\n <p class="title">\n <b>\n The demo python introduces several python courses.\n </b>\n </p>\n <p class="course">\n Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\n <a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">\n Basic Python\n </a>\n and\n <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">\n Advanced Python\n </a>\n .\n </p>\n </body>\n</html>'
- >>> print(soup.prettify()
- <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 class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">
- Basic Python
- </a>
- and
- <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">
- Advanced Python
- </a>
- .
- </p>
- </body>
- </html>
prettify()
还可用于标签,使用方法:<tag>.prettify()
- >>> print(soup.a.prettify())
- <a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">
- Basic Python
- </a>