2017-12-23 1 views
0
내가 웹에서 데이터를 긁어와 태그 'DIV'와 클래스가 모든 요소를 ​​제거하는 아래의이 HTML과 같은 '모듈 노트'있어

:요소를 제거 3

 <div class="notes module" role="complementary"> 
    <h3 class="heading">Notes:</h3> 
    <ul class="associations"> 
     <li> 
      Translation into Русский available: 
      <a href="/works/494195">Два-два-один Браво Бейкер</a> by <a rel="author" href="https://stackoverflow.com/users/dzenka/pseuds/dzenka">dzenka</a>, <a rel="author" href="https://stackoverflow.com/users/La_Ardilla/pseuds/La_Ardilla">La_Ardilla</a> 
     </li> 
    </ul> 
    <blockquote class="userstuff"> 
     <p> 
    <i>Warnings: numerous references to and glancing depictions of combat, injury, murder, and mutilation of the dead; deaths of minor and major original characters. Numerous explicit depictions of sex between two men.</i> 
</p> 
    </blockquote> 
    <p class="jump">(See the end of the work for <a href="#children">other works inspired by this one</a>.)</p> 
</div> 

출처는 다음과 같습니다.보기 원본 : http://archiveofourown.org/works/180121?view_full_work=true

삭제하려는 요소를 찾아서 인쇄하는 데 어려움을 겪고 있습니다. 지금까지 나는 가지고있다 :

import urllib.request, urllib.parse, urllib.error 
from lxml import html 
from bs4 import BeautifulSoup 

url = 'http://archiveofourown.org/works/180121?view_full_work=true' 
html = urllib.request.urlopen(url).read() 
soup = BeautifulSoup(html, 'lxml') 
removals = soup.find_all('div', {'id':'notes module'}) 
for match in removals: 
    match.decompose() 

그러나 제거는 빈 목록을 반환한다. 내가 위에서 선택한 모든 div 엘리먼트를 선택하여 html에서 이러한 모든 엘리먼트를 선택하고 제거하도록 도울 수 있습니까?

감사합니다.

답변

1

찾으려는 div의 코드는 class = "notes module"입니다. 코드에서 id = "notes module"으로 해당 div를 찾으려고합니다. 이에

removals = soup.find_all('div', {'id':'notes module'}) 

:이 줄을 변경

removals = soup.find_all('div', {'class':'notes module'}) 
+0

감사합니다. 그래도 여전히 빈 목록을 얻고 있습니다. – SBlack

0

는 한번 풀어 줘. 해당 웹 페이지의 을 모두 class='wrapper'에서 찾아냅니다.

import requests 
from bs4 import BeautifulSoup 

html = requests.get('http://archiveofourown.org/works/180121?view_full_work=true') 
soup = BeautifulSoup(html.text, 'lxml') 
for item in soup.select(".wrapper"): 
    [elem.extract() for elem in item("div")] 
    print(item)