2017-02-16 4 views
0

Ich verwende Beautifulsoup einen Künstlernamen von einem Blog zu holen, ein bestimmtes Spiel der Musik-Tags gegeben:BeautifulSoup - Rückkehr Kopf Korrespondent angepasst Fußzeile

import requests 
from bs4 import BeautifulSoup 

r = requests.get('http://musicblog.kms-saulgau.de/tag/chillout/') 
html = r.content 

soup = BeautifulSoup(html, 'html.parser') 

Künstlernamen werden hier gespeichert:

header = soup.find_all('header', class_= "entry-header") 

und Künstler Tags hier:

span = soup.find_all('span', class_= "tags-links") 

ich alle Header bekommen können:

for each in header: 
    if each.find("a"): 
     each = each.find("a").get_text() 
     print each 

Und dann suche ich nach oben für 'Alternative' und 'Chillout' in der gleichen Fußzeile:

for each in span: 
    if each.find("a"): 
     tags = each.find("a")["href"] 
     if "alternative" in tags:  
      print each.get_text() 

der Code, so weit, druckt:

Terra Nine – The Heart of the Matter 
Emmit Fenn – Blinded 
Amparo – The Orchid Glacier 
Alpha Minus – Satellites 
Carbonates on Mars – The Song of Sol 
Josey Marina – Ocean Sighs 
Sunday – Only 
Some Kind Of Illness – The Light 
Vesna Kazensky – Raven 
James Lowe – Shallow 

Tags Alternative, Chillout, Indie Rock, New tracks 

aber was ich versuche zu tun ist, nur den Eintrag entsprechend der passenden Fußzeile, so zurück:

Some Kind Of Illness – The Light 
Alternative, Chillout, Indie Rock, New tracks 

Wie kann ich das erreichen?

Antwort

0
for article in soup.find_all('article'): 
    if article.select('a[href*="alternative"]') and article.select('a[href*="chillout"]'): 
     print(article.h2.text) 
     print(article.find(class_='tags-links').text) 

aus:

Some Kind Of Illness – The Light 
Tags Alternative, Chillout, Indie Rock, New tracks 
Verwandte Themen