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 技術手冊 第三版
html.entitiesv2htmlentitydefs)模組
|
685
要取得從屬性名稱映射至值的一個
dict
(或定義在 HTML 標準中的少數情
況,是值所成的
list
),就使用屬性
t.attrs
>>> s = bs4.BeautifulSoup('<p foo="bar" class="ic">baz')
>>> s.get('foo')
>>> s.p.get('foo')
'bar'
>>> s.p.attrs
{'foo': 'bar', 'class': ['ic']}
如何檢查一個
Tag
實體是否有某個特定屬性
要檢查一個
Tag
實體
t
HTML 屬性是否包括一個名稱叫做
'foo'
的,請
使用
if 'foo' in t:
Tag
實體上的
in
運算
子會在
Tag
的子節點中查找,而
它的屬性中。取而代之,
請使用
if 'foo' in t.attrs:
if t.has_attr('foo'):
當你有
NavigableString
的一個實體,你經常會想要存取它所含有的實際
文字字串;當你有
Tag
的一個實體,你可能會想要存取它含有的唯一字串
unique string),或者,如果它含有多個的話,存取它們全部,可能還要
剝除圍住它們的任何空白。這裡是它們的做法。
取得一個實際的字串
如果你有一個
NavigableString
實體
s
,而你需要儲藏或處理它某處的文
字,而不想巡覽它,就呼叫
str(s)
(或在 v2 中,
unicode(s)
)。或者,
使用
s.encode(codec='utf8') ...
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