Skip to Content
Python 技術手冊 第三版
book

Python 技術手冊 第三版

by lex Martelli, Anna Martelli Ravenscroft, Steve Holden
January 2018
Intermediate to advanced content levelIntermediate to advanced
856 pages
17h 17m
Chinese
GoTop Information, Inc.
Content preview from Python 技術手冊 第三版
694
|
22 章 結構化文字:HTML
pieces = urlparse.urlparse(url)
if pieces[0]=='http':
print(urlparse.urlunparse(pieces))
v3 中,程式碼是相同的,只不過函式
urlopen
現在會是在模組
urllib.
request
中,而函式
urlparse
urlunparse
則是在模組
urllib.parse
中,
而非分別位在模組
urllib
urlparse
中,如 v2 中那樣(涵蓋於前面的
URL 存取」中),這個議題與
BeautifulSoup
本身無關。
這個範例呼叫類別
bs4.BeautifulSoup
的實體(等同於呼叫它的
find_all
法)來取得某個特定標記(在此為標記
'<a>'
)的所有實體,然後呼叫類別
Tag
的實體之
get
方法來取得一個屬性(在此為
'href'
)的值,或在缺少該
屬性時得到
None
產生 HTML
Python 並沒有專門用來產生 HTML 的工具,或能讓你直接在 HTML 頁面
內嵌 Python 程式碼的工具。開發與維護的工作因為透過
templating
(範
本)區分邏輯和呈現(presentation)議題而變得容易,涵蓋於後面的「使
用範本」中。一個替代做法是使用
bs4
來建立 HTML 文件,在你的 Python
程式碼中進行,從更動一個最初只有最底限部分的文件開始。既然這些更
動仰賴
bs4
剖析
parsing
一些 HTML,使用不同的剖析器(parsers
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

測試驅動開發|使用 Python

測試驅動開發|使用 Python

Harry J.W. Percival
Linux 内核观测技术BPF

Linux 内核观测技术BPF

David Calavera, Lorenzo Fontana

Publisher Resources

ISBN: 9789864766819