Beautiful Soup库入门
cmd安装指令:pip install beautifulsoup4
>>> 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>'
>>> demo = r.text
>>> from bs4 import 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>
BeautifulSoup类
BeautifulSoup库的解析器:
基本元素:
----------续上
>>> soup.title
<title>This is a python demo page</title>
>>> soup.a # 返回第一个a标签
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>
>>> soup.a.name # 标签a的名字
'a'
>>> soup.a.parent
<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>
>>> soup.a.parent.name # 标签a的上一级标签的名字
'p'
>>> soup.a.parent.parent.name
'body'
>>> soup.a.attrs # 标签属性,为字典类型
{'href': 'http://www.icourse163.org/course/BIT-268001', 'class': ['py1'], 'id': 'link1'}
>>> soup.a.attrs['class']
['py1']
>>> soup.a.string # 标签内的内容
'Basic Python'
>>> soup.p.string # 实际内部有一个b标签,说明该方法可以跨越多个层次
'The demo python introduces several python courses.'
>>> type(soup.p.string)
<class 'bs4.element.NavigableString'>
遍历:
html基本格式:
下行遍历:
--------续上
>>> soup.head
<head><title>This is a python demo page</title></head>
>>> soup.head.contents
[<title>This is a python demo page</title>]
>>> 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.contents)
5
>>> soup.body.contents[1]
<p class="title"><b>The demo python introduces several python courses.</b></p>
遍历儿孙节点用descendants而不是children
上行遍历:
平行遍历:
平行遍历只能发生在同一个父节点之下。
prettify()方法用于保持原格式打印html数据。
信息提取方法:
- 遍历
- 对文本使用查找函数
例如:提取所有URL链接
- 搜索所有<a>标签
- 解析<a>标签格式后提取href后的链接内容
>>> soup = BeautifulSoup(demo,'html.parser')
>>> for link in soup.find_all('a'):
... print(link.get('href'))
...
http://www.icourse163.org/course/BIT-268001
http://www.icourse163.org/course/BIT-1001870001
>>> soup.find_all('p', 'course')
[<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>]
>>> soup.find_all(id='link1')
[<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>]
>>> soup.find_all(id='link')
[]
>>> soup.find_all(id=re.compile('link'))
[<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>, <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>]
<>.find_all函数:
<>.find_all(name, attrs, recursive, string, **kwargs)
返回列表类型,存储查找结果
- name:查找<a>标签,查找多个参数如同时查找<a>和<b>时,将这个参数用列表形式表示即可如 [‘a’, ‘b’]。使用True时,返回所有标签名字。查找b开头的名字如b和body时,需要使用正则表达式库,import re导入正则表达式库,参数设为re.compile(‘b’),可以查找b开头所有信息
- attrs:对标签属性值检查字符串,可标注属性检索
- recursive:是否对子孙全部检索,默认True
- string:检索<>和</>之间的字符串,必须要完整精确,使用一部分时需要用正则表达式
- 扩展方法:
<tag>(…) 等价于 <tag>.find_all(…)
text属性:tag.text可以返回该标签下的所有文本信息,包括了该标签下子标签的文本信息。和string的不同就在于text可以返回所有文本,但string只能返回这一个标签的文本,当存在多个标签时,string会返回None。