Beautiful Soup库入门---Python网络爬虫和信息提取2(北理工mooc)

Beautiful Soup库入门

cmd安装指令:pip install beautifulsoup4

>>> import requests
>>> r = requests.get("https://python123.io/ws/demo.html")
>>> r.text
'This is a python demo page\r\n\r\n

The demo python introduces several python courses.

\r\n

Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\nBasic Python and Advanced Python.

\r\n'
>>> 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库的解析器:
Beautiful Soup库入门---Python网络爬虫和信息提取2(北理工mooc)_第1张图片
基本元素:
Beautiful Soup库入门---Python网络爬虫和信息提取2(北理工mooc)_第2张图片

----------续上
>>> 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基本格式:
Beautiful Soup库入门---Python网络爬虫和信息提取2(北理工mooc)_第3张图片
下行遍历:
Beautiful Soup库入门---Python网络爬虫和信息提取2(北理工mooc)_第4张图片

--------续上
>>> 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>

Beautiful Soup库入门---Python网络爬虫和信息提取2(北理工mooc)_第5张图片
遍历儿孙节点用descendants而不是children
上行遍历:
Beautiful Soup库入门---Python网络爬虫和信息提取2(北理工mooc)_第6张图片
Beautiful Soup库入门---Python网络爬虫和信息提取2(北理工mooc)_第7张图片
平行遍历:
Beautiful Soup库入门---Python网络爬虫和信息提取2(北理工mooc)_第8张图片
平行遍历只能发生在同一个父节点之下。
Beautiful Soup库入门---Python网络爬虫和信息提取2(北理工mooc)_第9张图片
prettify()方法用于保持原格式打印html数据。
信息提取方法:

  • 遍历
  • 对文本使用查找函数
    例如:提取所有URL链接
  1. 搜索所有标签
  2. 解析标签格式后提取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)
返回列表类型,存储查找结果

(…) 等价于 .find_all(…)
text属性:tag.text可以返回该标签下的所有文本信息,包括了该标签下子标签的文本信息。和string的不同就在于text可以返回所有文本,但string只能返回这一个标签的文本,当存在多个标签时,string会返回None。

你可能感兴趣的:(Python爬虫笔记)