2016-03-23 5 views
2

ich ein HTML-Dokument haben, wie folgt:Ausschneiden/Slicing eines HTML-Dokuments in Stücke mit BeautifulSoup?

<h1> Name of Article </h2> 
<p>First Paragraph I want</p> 
<p>More Html I'm interested in</p> 
<h2> Subheading in the article I also want </h2> 
<p>Even more Html i want to pull out of the document.</p> 
<h2> References </h2> 
<p>Html I do not want...</p> 

I Referenzen müssen nicht aus dem Artikel, möchte ich das Dokument in der zweiten h2-Tag schneiden.

Natürlich kann ich eine Liste von h2-Tags wie so finden:

soup = BeautifulSoup(html) 
soupset = soup.find_all('h2') 
soupset[1] #this would get the h2 heading 'References' but not what comes before it 

Ich will nicht eine Liste der h2-Tags erhalten, mag ich das Dokument direkt an dem zweiten h2-Tag in Scheiben schneiden und behalte den obigen Inhalt in einer neuen Variable. Grundsätzlich ist die gewünschte Ausgabe ich will, ist:

<h1> Name of Article </h2> 
<p>First Paragraph I want<p> 
<p>More Html I'm interested in</p> 
<h2> Subheading in the article I also want </h2> 
<p>Even more Html i want to pull out of the document.</p> 

Was ist der beste Weg, dies zu tun aboout „Slicing“/Schneiden des HTML-Dokuments zu gehen, anstatt einfach Tags zu finden und outputing die Tags selbst?

Antwort

1

können Sie remove/extract jedes Geschwisterelement des "References" Element und das Element selbst:

import re 
from bs4 import BeautifulSoup 

data = """ 
<div> 
    <h1> Name of Article </h2> 
    <p>First Paragraph I want</p> 
    <p>More Html I'm interested in</p> 
    <h2> Subheading in the article I also want </h2> 
    <p>Even more Html i want to pull out of the document.</p> 
    <h2> References </h2> 
    <p>Html I do not want...</p> 
</div> 
""" 
soup = BeautifulSoup(data, "lxml") 

references = soup.find("h2", text=re.compile("References")) 
for elm in references.find_next_siblings(): 
    elm.extract() 
references.extract() 

print(soup) 

Drucke:

<div> 
    <h1> Name of Article</h1> 
    <p>First Paragraph I want</p> 
    <p>More Html I'm interested in</p> 
    <h2> Subheading in the article I also want </h2> 
    <p>Even more Html i want to pull out of the document.</p> 
</div> 
+0

Ich denke, das sollte funktionieren, danke !! Sehr geschätzt. Sehr sauber. Nur um sicher zu gehen, dass ich es klar verstanden habe, ist die Schleife, die elm.extract() ausführt, notwendig, um den ganzen HTML-Code innerhalb der h2-Tags zu entfernen, richtig? Dann das Ende references.extract() entfernt einfach die "References" h2 Tags, nachdem alles aus ihm extrahiert wird? – EazyC

+1

@EazyC Ich hoffe es hilft. In der Schleife entfernen wir jedes nächste Geschwister in das References-Element, dann entfernen wir das References-Element selbst. – alecxe

+0

Das ist, wie ich dachte, danke! – EazyC

0

Sie können die Position des h2 im String finden und dann Finden Sie einen Teilstring von ihm:

last_h2_tag = str(soup.find_all("h2")[-1]) 
html[:html.rfind(last_h2_tag) + len(last_h2_tag)]