python——爬取网页BeautifulSoup的三种遍历(4)

本文介绍了如何使用Beautiful Soup库安装及遍历HTML页面。内容包括库的安装、基本元素、prettify()函数以及下行、上行和平行遍历的详细解释和示例。
摘要由CSDN通过智能技术生成

Beautiful Soup库的安装

【安装】:

  1. 使用管理员权限打开cmd
  2. 输入pip install beautifulsoup4
    在这里插入图片描述
    3.测试Beautiful Soup 库是否安装完成
    示例网站:https://python123.io/ws/demo.html
    页面源代码:
    在这里插入图片描述
    (1)使用requests库提取网页
>>> import requests
>>> r=requests.get("https://python123.io/ws/demo.html")
>>> r.status_code
200
>>> r.encoding=r.apparent_encoding
>>> 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
>>> 

在这里插入图片描述
(2)使用Beautiful Soup库

>>> from bs4 import BeautifulSoup #从之前安装的beautifulsoup4中导入BeautifulSoup类
>>> soup=BeautifulSoup(demo,"html.parser") #使用html.parser解释器,对demo进行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>
>>> 

在这里插入图片描述

Beautiful Soup库的基本元素

1.定义:Beautiful Soup库是解析、遍历、维护“标签树”的功能库
标签:1.名称成对出现
2.属性0个或多个,由键值对构成
【例】:<p class="title">...</p> 名称:p 属性:class="title"
2.引用Beautiful Soup库的方法

from bs4 import BeautifulSoup

注意 BeautifulSoup中,字母“B”和字母“S”要大写
或者:

import bs4

3.调用Beautiful Soup库的格式

soup=BeautifulSoup("<html>data</html>","html.parser")
soup2=BeautifulSoup(open("D://demo.html"),"html.parser")

4.Beautiful Soup的解析器
在这里插入图片描述
4.Beautiful Soup的基本元素
在这里插入图片描述

>>> import requests
>>> r=requests.get("https://python123.io/ws/demo.html")
>>> demo=r.text
>>> from bs4 import BeautifulSoup
>>> soup=BeautifulSoup(demo,"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())
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值