2016-09-13 3 views
0

Wie finde ich alle Links mit 3 Attributen mit schöner Suppe?Schöne Suppe. So finden Sie alle Links mit 3 Attributen

würde Ich mag alle Links finden, die alle Attribute sind:

a id="js_24" class="_27jf _3emk" data-hover="tooltip" 

Ich habe versucht, eine solche Art und Weise:

emo = soup.find_all('a', {'id': 'fjs_24', 'class': '_27jf _3emk', 'data-hover':"tooltip"}) 

aber keinen Erfolg. Ich bekomme keine Ergebnisse.

Voll Link sieht so:

<a id="js_24" class="_27jf _3emk" data-hover="tooltip" aria-label="6 Wow" href="/ufi/reaction/profile/browser/?ft_ent_identifier=909182312524600&av=100011414120311" rel="ignore" role="button" tabindex="-1"> 
+0

ist die ID nicht eindeutig zuzuordnen? –

+0

Nein, es ist nicht so einfach;) –

Antwort

1

Es gibt nichts falsch mit Ihrer Logik ist, das Problem ist die id falsch ist, Sie fjs_24 haben, wenn die tatsächliche ID ist js_24:

emo = soup.find_all('a', {'id': 'js_24', 'class': '_27jf _3emk', 'data-hover':"tooltip"}) 

Sie können sehen, dass es funktioniert, sobald Sie die Änderung vornehmen:

In [10]: from bs4 import BeautifulSoup 


In [11]: soup = BeautifulSoup("""<a id="js_24" class="_27jf _3emk" data-hover="tooltip" aria-label="6 Wow" href="/ufi/reaction/profile/browser/?ft_ent_identifier=909182312524600&av=100011414120311" rel="ignore" role="button" tabindex="-1"></a>""","lxml") 


In [12]: soup.find_all('a', {'id': 'fjs_24', 'class': '_27jf _3emk', 'data-hover':"tooltip"}) 
Out[12]: [] 

In [13]: soup.find_all('a', {'id': 'js_24', 'class': '_27jf _3emk', 'data-hover':"tooltip"}) 
Out[13]: [<a aria-label="6 Wow" class="_27jf _3emk" data-hover="tooltip" href="/ufi/reaction/profile/browser/?ft_ent_identifier=909182312524600&amp;av=100011414120311" id="js_24" rel="ignore" role="button" tabindex="-1"></a>] 

Wenn Sie lxml installiert haben, können Sie es tun viel schneller und prägnanter mit einem CSS-Selektor:

from lxml import html 

tree = html.fromstring(""""<a id="js_24" class="_27jf _3emk" data-hover="tooltip" aria-label="6 Wow" href="/ufi/reaction/profile/browser/?ft_ent_identifier=909182312524600&av=100011414120311" rel="ignore" role="button" tabindex="-1"></a>""") 

print(tree.cssselect("#js_24[class='_27jf _3emk'][data-hover='tooltip']")) 
+0

Danke :) Aber wie man es ausdruckt? für em in emo: print em.text funktioniert nicht –

+0

@AnnaK, wenn Sie nur für eine Verwendung suchen finden Sie anstelle von find_all, find_all sucht nach mehreren Übereinstimmungen und gibt eine Liste/ResultSet –

+0

Wenn Sie den Text von mehrere dann nur '[a.text für ein in emo]' –