a12*_*773 3 python beautifulsoup python-2.7
我有这个范围,我想获得冠军
<span title="Something"></span>
Run Code Online (Sandbox Code Playgroud)
怎么用beautifulsoup得到它?
res = soup.find('span')
print res //Was trying to add res.title but result is 'None'
Run Code Online (Sandbox Code Playgroud)
Gro*_*mer 11
您应该能够像这样访问它:
res = soup.find('span')['title']
编辑:我shoudl澄清,res将是title属性的值.如果您希望稍后使用该元素,请将我的代码更改为:
res = soup.find('span')
title = res['title']
Run Code Online (Sandbox Code Playgroud)
然后你可以继续使用res(如果需要).
此外,.find将返回单个元素.您需要确保它是您想要的范围,因为HTML可能有多个范围.