Python获取一个静态网页的内容

这是一个简单的html页面,请保持为字符串,完成后面的计算要求。

 

from bs4 import BeautifulSoup
import re
html = '''
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>
<h1>我的第一个标题</h1>
<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>
'''
content = BeautifulSoup(html, "html.parser")
ChineseCharcter = re.findall(u'[\u1100-\uFFFDh]+?',content.text)

print('head标签内容:\n',content.title,"\n\n学号后两位:\n24")
print('\nbody标签内容:\n',content.body)
print('\nid为first的标签对象:\n',content.find_all(id='first'))
print("\nhtml页面中的中文字符:\n",ChineseCharcter)

 

posted @ 2020-12-14 14:53  英魂  阅读(381)  评论(0)    收藏  举报