【发布时间】:2016-01-12 21:11:54
【问题描述】:
我正在尝试从 BBC 体育页面中提取主要标题(当前:“温格预测 ' 活跃' 1 月”)。 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
任何帮助将不胜感激。谢谢:)
【问题讨论】:
标签: python python-2.7 webpage python-2.x bs4