python中beautifulSoup库的安装和使用

beautifulsoup库是什么?
beautifulsoup库是解析、遍历、维护“标签树”的功能库
1.安装库
管理员方式运行cmd,输入pip install beautifulsoup4
2.使用

>>> import requests
>>> r=requests.get("https://python123.io/ws/demo.html")
>>> r.text

此时没有运用到beautifulsoup库,输出结果

'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库的基本元素

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