2016-08-12 4 views
0

나는 <a> 태그의 형제 구조를 걸어 내려고하고 있으며 <br> 개의 태그가 있습니다. br 태그의 elem.name을 가져 오려고하면 오류가 발생합니다. 이 br 태그를 건너 뛸 수있는 방법이 있습니까?<br> 태그를 확인하십시오

현재 구문 분석을하기 전에 html = html.replace('<br>','\n')을 수행하지만 bsoup가 줄 바꿈에^M 문자를 삽입합니다.

r = requests.get(url, headers=headers) 
    # page = r.text.replace('<br>','\n') 
    soup = bsoup(r.text, 'html.parser') 
    soup = soup.find('div', id='listAlbum') 
    albums = soup.find_all('div', class_='album') 
    for album in albums: 
      name = album.text.replace('"','').replace(':','').rstrip() 
      print(name) 
      albumtask(name) 
      song = album.next_sibling 
      while song.name != 'div' and song.name != 'script': 
        if song.name != 'a' or song.get('id'): 
          song = song.next_sibling 
          continue 
        t = threading.Thread(target=tsong, args=(song,)) 
        t.start() 
        song = song.next_sibling 
        while song.is_empty_element: 
          song = song.next_sibling 
        time.sleep(0.2) 

 

<div id="listAlbum"> 
<a id="1545"></a><div class="album">album: <b>"Pablo Honey"</b> (1993)<span>&nbsp;&nbsp;<a href="http://www.amazon.com/gp/search?ie=UTF8&amp;keywords=RADIOHEAD+Pablo+Honey&amp;tag=azlyricsunive-20&amp;index=music&amp;linkCode=ur2&amp;camp=1789&amp;creative=9325" rel="external"><img width="30" height="18" src="http://images.azlyrics.com/amn.png" alt="buy this CD or download MP3s at amazon.com!"></a></span></div> 
<a href="../lyrics/radiohead/you.html" target="_blank">You</a><br> 
<a href="../lyrics/radiohead/creep.html" target="_blank">Creep</a><br> 
<a href="../lyrics/radiohead/howdoyou.html" target="_blank">How Do You?</a><br> 
<a href="../lyrics/radiohead/stopwhispering.html" target="_blank">Stop Whispering</a><br> 
<a href="../lyrics/radiohead/thinkingaboutyou.html" target="_blank">Thinking About You</a><br> 
<a href="../lyrics/radiohead/anyonecanplayguitar.html" target="_blank">Anyone Can Play Guitar</a><br> 
<a href="../lyrics/radiohead/ripcord.html" target="_blank">Ripcord</a><br> 
<a href="../lyrics/radiohead/vegetable.html" target="_blank">Vegetable</a><br> 
<a href="../lyrics/radiohead/proveyourself.html" target="_blank">Prove Yourself</a><br> 
<a href="../lyrics/radiohead/icant.html" target="_blank">I Can't</a><br> 
<a href="../lyrics/radiohead/lurgee.html" target="_blank">Lurgee</a><br> 
<a href="../lyrics/radiohead/blowout.html" target="_blank">Blow Out</a><br> 

<a id="1543"></a><div class="album">EP: <b>"My Iron Lung"</b> (1994)<span>&nbsp;&nbsp;<a href="http://www.amazon.com/gp/search?ie=UTF8&amp;keywords=RADIOHEAD+My+Iron+Lung&amp;tag=azlyricsunive-20&amp;index=music&amp;linkCode=ur2&amp;camp=1789&amp;creative=9325" rel="external"><img width="30" height="18" src="http://images.azlyrics.com/amn.png" alt="buy this CD or download MP3s at amazon.com!"></a></span></div> 
<a href="../lyrics/radiohead/myironlung.html" target="_blank">My Iron Lung</a><br> 

그리고 그런 식으로 계속

.

+0

(내가 파이썬이나 아름다운 수프의 아무것도 몰라) 만에 HTML 조건, 아마도 좋은 시간은
년대를 제거하고 사용으로 이동 요소 사이에 간격/안쪽 여백을 만들려면 CSS를 사용하십시오 (의 경우 "display : block"사용). 표시 목적으로 줄 바꿈을 사용하는 것은 표시 목적으로 사용되는 CSS로 더 잘 수행됩니다. – gavgrif

+0

오케이. 이 경우에는 HTML이 아닙니다. – user193661

+0

@ user193661이 샘플에 대해 샘플 입력 HTML과 원하는 출력을 제공 할 수 있습니까? 정말 도움이 될 것입니다. – alecxe

답변

1

나는 모든 앨범을 먼저 반복합니다. 이것은 #listAlbum .album CSS 선택기와 일치하는 요소입니다. 이제 모든 앨범에 대해 find all a following siblings을 반복하여 노래 제목을 수집합니다. id이있는 요소가 발생하면 중단됩니다. 구현 :

from collections import defaultdict 
from pprint import pprint 

from bs4 import BeautifulSoup 


data = """ 
<div id="listAlbum"> 
    <a id="1545"></a><div class="album">album: <b>"Pablo Honey"</b> (1993)<span>&nbsp;&nbsp;<a href="http://www.amazon.com/gp/search?ie=UTF8&amp;keywords=RADIOHEAD+Pablo+Honey&amp;tag=azlyricsunive-20&amp;index=music&amp;linkCode=ur2&amp;camp=1789&amp;creative=9325" rel="external"><img width="30" height="18" src="http://images.azlyrics.com/amn.png" alt="buy this CD or download MP3s at amazon.com!"></a></span></div> 
    <a href="../lyrics/radiohead/you.html" target="_blank">You</a><br> 
    <a href="../lyrics/radiohead/creep.html" target="_blank">Creep</a><br> 
    <a href="../lyrics/radiohead/howdoyou.html" target="_blank">How Do You?</a><br> 
    <a href="../lyrics/radiohead/stopwhispering.html" target="_blank">Stop Whispering</a><br> 
    <a href="../lyrics/radiohead/thinkingaboutyou.html" target="_blank">Thinking About You</a><br> 
    <a href="../lyrics/radiohead/anyonecanplayguitar.html" target="_blank">Anyone Can Play Guitar</a><br> 
    <a href="../lyrics/radiohead/ripcord.html" target="_blank">Ripcord</a><br> 
    <a href="../lyrics/radiohead/vegetable.html" target="_blank">Vegetable</a><br> 
    <a href="../lyrics/radiohead/proveyourself.html" target="_blank">Prove Yourself</a><br> 
    <a href="../lyrics/radiohead/icant.html" target="_blank">I Can't</a><br> 
    <a href="../lyrics/radiohead/lurgee.html" target="_blank">Lurgee</a><br> 
    <a href="../lyrics/radiohead/blowout.html" target="_blank">Blow Out</a><br> 

    <a id="1543"></a><div class="album">EP: <b>"My Iron Lung"</b> (1994)<span>&nbsp;&nbsp;<a href="http://www.amazon.com/gp/search?ie=UTF8&amp;keywords=RADIOHEAD+My+Iron+Lung&amp;tag=azlyricsunive-20&amp;index=music&amp;linkCode=ur2&amp;camp=1789&amp;creative=9325" rel="external"><img width="30" height="18" src="http://images.azlyrics.com/amn.png" alt="buy this CD or download MP3s at amazon.com!"></a></span></div> 
    <a href="../lyrics/radiohead/myironlung.html" target="_blank">My Iron Lung</a><br> 
</div>""" 

soup = BeautifulSoup(data, "html5lib") 
albums = defaultdict(list) 
for album in soup.select("#listAlbum .album"): 
    album_title = album.get_text().strip() 
    for song in album.find_next_siblings("a"): 
     if "id" in song.attrs: 
      break 

     song_title = song.get_text(strip=True) 
     albums[album_title].append(song_title) 

pprint(dict(albums)) 

인쇄 :

{'EP: "My Iron Lung" (1994)': ['My Iron Lung'], 
'album: "Pablo Honey" (1993)': ['You', 
           'Creep', 
           'How Do You?', 
           'Stop Whispering', 
           'Thinking About You', 
           'Anyone Can Play Guitar', 
           'Ripcord', 
           'Vegetable', 
           'Prove Yourself', 
           "I Can't", 
           'Lurgee', 
           'Blow Out']} 
주제 오프 약간
관련 문제