python爬虫里的换行prettify

时间:2024-03-12 21:36:16
>>> print(soup.p.prettify)
<bound method Tag.prettify of <p class="title"><b>The demo python introduces several python courses.</b></p>>
>>> print(soup.p.prettify())
<p class="title">
 <b>
  The demo python introduces several python courses.
 </b>
</p>

>>> print(soup.html.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>
>>> print(soup.head.prettify())
<head>
 <title>
  This is a python demo page
 </title>
</head>

>>>

工作原理,在原来的文本里加入了换行符:“\n”。就可以进行换行,使得程序一目了然!

图片比例不对,就用放大镜查看!python爬虫里的换行prettify

python爬虫里的换行prettify