import os, re, sys, urllib2
from bs4 import BeautifulSoup
import lxml
def get_epg(channel, html):
soup = BeautifulSoup(html, "lxml")
main_div = soup.find("div", {"class":"viewport-container"})
elements = main_div.find_all("li")
for element in elements:
cmp = element.find("div", { "class" : "channel" } ).getText()
#return cmp
if channel == cmp:
print "found"
return element
EPG_URL = "http://www.hoerzu.de/tv-programm/jetzt/"
html = urllib2.urlopen(EPG_URL)
print get_epg("ZDF", html)
结果是:
Traceback (most recent call last):
File "epg.py", line 17, in <module>
print get_epg("ZDF", html)
File "epg.py", line 10, in get_epg
cmp = element.find("div", { "class" : "channel" } ).getText()
AttributeError: 'NoneType' object has no attribute 'getText'
我真的不明白这里有什么问题,因为当我这样做时:
for element in elements:
cmp = element.find("div", { "class" : "channel" } ).getText()
return cmp
错误没有出现,一切都按预期进行......