赞
踩
使用.text或.string属性获取元素的文本内容.
使用.get(‘attrname’)或[‘attrname’]来获取属性值.
html = '''
This is title'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(html)
print('name={} value={}'.format('title', soup.title.text)) #
print('name={} value={}'.format('link', soup.link['href'])) #
输出:
name=title value= This is title
name=link value=.../style.css
根据OP的评论更新:
def get_text(el): return el.text
def get_href(el): return el['href']
# map tag names to functions (what to retrieve from the tag)
what_todo = {
'title': get_text,
'link': get_href,
}
for el in soup.select('head *'): # To retrieve all children inside `head`
f = what_todo.get(el.name)
if not f: # skip non-title, non-link tags.
continue
print('name={} value={}'.format(el.name, f(el)))
输出:与上述相同
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。