'NavigableString' object has no attribute 'a'
Wenn ich es wie diese versuchen
def get_vlad(url):
html = requests.get(url, headers=headers)
soup = BeautifulSoup(html.text, 'html5lib')
divs = soup.findAll('section', 'videos')[0]
entries = [{'text': div.strong.a.text,
'link': div.a.get('href'),
'img': div.find('img').get('src')
} for div in divs]
return entries
hat ich erhalte diese Fehler
'NavigableString' object has no attribute 'a'
aber wenn ich es wie folgt
def get_vlad(url):
html = requests.get(url, headers=headers)
soup = BeautifulSoup(html.text, 'html5lib')
divs = soup.findAll('section', 'videos')
entries = [{'text': div.strong.a.text,
'link': div.a.get('href'),
'img': div.find('img').get('src')
} for div in divs]
return entries
Es funktioniert. Aber es gibt ungefähr 4
<div class="video">
divs. So dauert es das erste Video von jedem
<div class="video">
Abschnitt. Aber ich möchte alle Videos im selben div durchlaufen. Also habe ich diese
divs = soup.findAll('section', 'videos')[0]
aber das gibt mir den Fehler, damit ich diesen versuchte
divs = soup.find('section', 'videos')
Zu denken, dass es das erste Video div und eine Schleife durch alle diese divs zielen würde, aber das hat nicht funktioniert heres die hTML-Struktur ich Schaben
<section class="video">
<section class="box">
<a href="/videos/video.php?v=wshhO9CO5349voIH6k6b" class="video-box">
<img src="http://hw-static.example.org/u/pic/2016/05/2cuPh1LdFzJo.jpg" width="222" height="125" alt="">
</a>
<strong class="title"><a href="/videos/video.php?v=wshhO9CO5349voIH6k6b">Stays Wildin: Blac Youngsta Showing Out With His Money!</a></strong>
<div>
<span class="views">318,105</span>
<span class="comments"><a href="http://www.example.org/videos/video.php?v=wshhO9CO5349voIH6k6b#disqus_thread" data-disqus-identifier="94122">3717</a></span>
</div>
</section>
<section class="box">
<a href="/videos/video.php?v=wshhO9CO5349voIH6k6b" class="video-box">
<img src="http://hw-static.example.org/u/pic/2016/05/2cuPh1LdFzJo.jpg" width="222" height="125" alt="">
</a>
<strong class="title"><a href="/videos/video.php?v=wshhO9CO5349voIH6k6b">Stays Wildin: Blac Youngsta Showing Out With His Money!</a></strong>
<div>
<span class="views">318,105</span>
<span class="comments"><a href="http://www.example.org/videos/video.php?v=wshhO9CO5349voIH6k6b#disqus_thread" data-disqus-identifier="94122">3717</a></span>
</div>
</section>
<section class="box">
<a href="/videos/video.php?v=wshhO9CO5349voIH6k6b" class="video-box">
<img src="http://hw-static.example.org/u/pic/2016/05/2cuPh1LdFzJo.jpg" width="222" height="125" alt="">
</a>
<strong class="title"><a href="/videos/video.php?v=wshhO9CO5349voIH6k6b">Stays Wildin: Blac Youngsta Showing Out With His Money!</a></strong>
<div>
<span class="views">318,105</span>
<span class="comments"><a href="http://www.example.org/videos/video.php?v=wshhO9CO5349voIH6k6b#disqus_thread" data-disqus-identifier="94122">3717</a></span>
</div>
</section>
</section>
<section class="video"></section>
<section class="video"></section>
möchte ich durch die erste
iterierenund ziehen Sie alle Videos von dort aus. Was momentan mit meinem Code passiert, ist, dass ich das erste Video von jedem Videoabschnitt mache, was ich nicht machen möchte. Warum also wird das nicht
divs = soup.findAll('section', 'videos')[0]
oder diese
divs = soup.find('section', 'videos')
Arbeit?
danke für die Antwort Ich werde das jetzt versuchen. und die Klasse ist Videos, eine Kopie und Einfügen Fehler – nothingness
@nothingness, versuchen Sie die Bearbeitung zuerst und sehen, ob das ist, was Sie eigentlich wollen –
es funktioniert nicht. Und ist diese Syntax korrekt in Ihrem Code ("section.video section.box") Ich habe das nie in allen Beispielen gesehen, die ich gesehen habe – nothingness