简体   繁体   中英

Strange error with AttributeError: 'NoneType' object has no attribute 'getText'

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)

results in:

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'

I really don't get what is wrong here, because when I do:

    for element in elements:
        cmp = element.find("div", { "class" : "channel" } ).getText()
        return cmp

the error doesn't shows up and all goes as expected ...

The second iteration clearly returns None.

<div class="channel">Das Erste</div>
None
None
None
<div class="channel">ZDF</div>
None
None
None
<div class="channel">RTL</div>
None
None
None
<div class="channel">Sat.1</div>
None
None
None
<div class="channel">ProSieben</div>
None
None
None
<div class="channel">kabel eins</div>
None
None
None
<div class="channel">RTL II</div>
None
None
None
<div class="channel">VOX</div>
None
None
None
<div class="channel">Arte</div>
None
None
None
<div class="channel">3sat</div>
None
None
None
<div class="channel">Super RTL</div>
None
None
None
<div class="channel">KiKA</div>
None
None
None
<div class="channel">NDR</div>
None
None
None
<div class="channel">WDR</div>
None
None
None
<div class="channel">MDR</div>
None
None
None
<div class="channel">BR</div>
None
None
None
<div class="channel">SWR</div>
None
None
None
<div class="channel">HR</div>
None
None
None
<div class="channel">RBB</div>
None
None
None
<div class="channel">n-tv</div>
None
None
None
<div class="channel">N24</div>
None
None
None
<div class="channel">Servus TV</div>
None
None
None
<div class="channel">SPORT1</div>
None
None
None
<div class="channel">TV.Berlin</div>
None
None
None
<div class="channel">Hamburg 1</div>
None
None
None
<div class="channel">Eurosport</div>
None
None
None
<div class="channel">München TV</div>
None
None
None
<div class="channel">Franken Fernsehen</div>
None
None
None
<div class="channel">Tele 5</div>
None
None
None
<div class="channel">Das VIERTE</div>
None
None
None
<div class="channel">NRW TV</div>
None
None
None
<div class="channel">Nickelodeon / Comedy Central</div>
None
None
None

So you have to check for this condition instead of calling blindly getText().

from bs4 import BeautifulSoup

you should use "main_div.findAll"

for Bs4: find_all ---> findAll

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM