2016-05-10 5 views
0

Ich bin verwirrt, warum ichWarum bekomme ich immer 'NoneType' Objekt hat kein Attribut 'A' in der Django App?

'NoneType' object has no attribute 'a' 

bekommen Dies ist die HTML-Struktur I

<section class ="videos" 
<section class="box"> 
<a href="/videos/video.php?v=wshhH0xVL2LP4hFb0liu" class="video-box"> 
    <img src="http://hw-static.exampl.net/.jpg" width="222" height="125" alt=""> 
</a> 
<strong class="title"><a href="/videos/video.php?v=wshhH0xVL2LP4hFb0liu">Teen "Allegedly" </a></strong> 
<div> 
    <span class="views">11,323</span> 
    <span class="comments"><a href="http://www.example.net/v" data-disqus-identifier="94137">44</a></span> 
</div> 

in meinem Django app bin Schaben. Wenn ich dies tue

html = requests.get(vlad_url) 
     soup = BeautifulSoup(html.text, 'html.parser') 
     divs = soup.find('section', 'videos') 

     img = divs.find('img').get('src') 
     text = divs.strong.a.text 
     link = divs.a.get('href') 

context = { 
    "ref": link, 
    "src": img, 
    "txt": text, 
} 

in meinen Ansichten. Und das in meiner Vorlage

{{ref}} 
{{src}} 
{{txt}} 

Ich werde ein einziges Ergebnis für jeden bekommen. Aber wenn ich versuche, sie wie folgt zu durchlaufen

def get_vlad(url): 
     html = requests.get(url, headers=headers) 
     soup = BeautifulSoup(html.text, 'html.parser') 
     divs = soup.findAll('section', 'box') 

     entries = [{'text': div.strong.a.text, 
        'link': div.a.get('href'), 
        'img': div.find('img').get('src') 
         } for div in divs] 
     return entries 

Ich bekomme diesen Nonetype-Fehler, der seltsam ist, weil es existiert. Es ist auch seltsam aus dem Grund habe ich eine andere Schleife ähnlich wie diese, die

def get_data(uri): 

     html = requests.get(uri, headers=headers) 
     soup = BeautifulSoup(html.text, "html.parser") 
     divs = soup.findAll('div', 'thumbnail') 
     entries = [{'text': div.text, 
        'href': div.find('a').get('href'), 
        'src': div.find('img').get('src') 
        } for div in divs][:6] 
     return entries 

und dies ist die HTML-Struktur funktioniert auf

<div class="col-xs-12 col-md-4" id="split"> 
     <div class="thumbnail thumb"> 

      <h6 id="date">May 6, 2016</h6> 

      <img src="http://www.paraguayhits.com/wp-content/uploads/2015/11/Almighty-Ft.-N%CC%83engo-Flow-Por-Si-Roncan-660x330.jpg" class="img-responsive post"> 


     <div style="border-bottom: thin solid lightslategray; padding-bottom: 15px;"></div> 

     <div class="caption" id="cap"> 
      <a href="/blog/almighty-por-si-roncan-ft-nengo-flow-official-video/"> 
       <h5 class="post-title" id="title">Almighty - Por Si Roncan (ft. Ñengo Flow) [Official Video]</h5> 
      </a> 





      <p> 
       <a href="/blog/76/delete/" class="btn" role="button">delete</a> 
       <a href="/blog/almighty-por-si-roncan-ft-nengo-flow-official-video/edit/" class="btn" role="button">edit</a> 
      </p> 

     </div> 
    </div> 

was ist der Unterschied zwischen den beiden funktioniert? Wie kann ich eine Schleife durch meine Ergebnisse

+1

Ich bin auf meinem Web Zeug, aber ist '' tatsächlich gültig? –

Antwort

1

Die html gebrochen wird, sind die Kapitel-Tags ein Chaos, ich habe Erfolg mit html5lib hatte schlecht gebrochen html mit BS4 zum Parsen:

In [21]: h = """<section class="videos" 
    ....: <section class="box"> 
    ....: <a href="/videos/video.php?v=wshhH0xVL2LP4hFb0liu" class="video-box"> 
    ....:  <img src="http://hw-static.exampl.net/.jpg" width="222" height="125" alt=""> 
    ....: </a> 
    ....: <strong class="title"><a href="/videos/video.php?v=wshhH0xVL2LP4hFb0liu">Teen "Allegedly" </a></strong> 
    ....: <div> 
    ....:  <span class="views">11,323</span> 
    ....:  <span class="comments"><a href="http://www.example.net/v" data-disqus-identifier="94137">44</a></span> 
    ....: </div>""" 

In [22]: from bs4 import BeautifulSoup 

In [23]: soup = BeautifulSoup(h, 'html5lib') 

In [24]: divs = soup.select_one('section.videos') 

In [25]: img = divs.find('img').get('src') 

In [26]: text = divs.strong.a.text 

In [27]: link = divs.a.get('href') 

In [28]: img 
Out[28]: u'http://hw-static.exampl.net/.jpg' 

In [29]: text 
Out[29]: u'Teen "Allegedly" ' 

In [30]: link 
Out[30]: u'/videos/video.php?v=wshhH0xVL2LP4hFb0liu' 

Die richtige html sollte etwa wie folgt aussehen:

<section class ="videos"> 
     <section class="box"> 
      <a href="/videos/video.php?v=wshhH0xVL2LP4hFb0liu" class="video-box"> 
       <img src="http://hw-static.exampl.net/.jpg" width="222" height="125" alt=""> 
      </a> 
      <strong class="title"><a href="/videos/video.php?v=wshhH0xVL2LP4hFb0liu">Teen "Allegedly" </a></strong> 
     </section> 
     <div> 
      <span class="views">11,323</span> 
      <span class="comments"><a href="http://www.example.net/v" data-disqus-identifier="94137">44</a></span> 
     </div> 
</section 
+0

das hat in meiner Situation nicht funktioniert. Es gibt nichts in meiner Vorlage, es erkennt nicht html5lib und ich fragte, wie Sie Schleife und II sah keine Erwähnung der ... – nothingness

+0

@nothingness, müssen Sie html5lib installieren, was meinst du mit * wie schleift du *? –

Verwandte Themen