用Beautifulsoup4库处理html网站

代码:

from bs4 import BeautifulSoup 
r='''<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title> 
</head>
<body>
         <hl>我的第一个标题</hl>
         <p id="first">我的第一个段落。</p> 
</body>
                  <table border="1">
          <tr>
                  <td>row 1, cell 1</td> 
                  <td>row 1, cell 2</td> 
         </tr>
         <tr>
                  <td>row 2, cell 1</td>
                  <td>row 2, cell 2</td>
         <tr>
</table>
</html>'''
soup= BeautifulSoup(r)
print("打印head标签内容:")
print(soup.head)
print("我的学号:3029")
print("获取body标签内容:")
print(soup.body)
print("获取id为first的标签:")
print(soup.find_all(id="first"))
print("获取并打印html页面中的中文字符:")
print(soup.title.string)
print(soup.hl.string)
print(soup.p.string)

 结果:

原文地址:https://www.cnblogs.com/jiana/p/12884619.html