我试图从BBC体育版上调出主要标题(目前是:“温格预测‘活跃’的一月”)。ID是'lead caption',它位于<h2>
和<a>
标记中。我在用Python。你知道吗
from bs4 import BeautifulSoup
import urllib2
url = urllib2.urlopen("http://www.bbc.co.uk/sport/football/teams/arsenal")
soup=BeautifulSoup(url.read())
#Things I've tried
headline=soup.find('a', attrs={'id': 'lead-caption'})
print headline
#The above prints 'None'
headline1=soup.find('lead-caption').getText()
print headline1
#The above print "'NoneTpye' Object has no attirbute 'getText'
tag = soup.a
tag ['id'] = 'lead-caption'
type(tag)
print tag.string
#Error: NoneType object does not support item assignment
任何帮助都将不胜感激。谢谢:)
你的代码几乎是正确的,你在错误的元素中寻找,这就是为什么你得到
None
,它应该是div
:输出:
相关问题 更多 >
编程相关推荐