1. 程式人生 > 實用技巧 >Python獲取一個靜態網頁的內容

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)