2014-03-12 22 views
0

我用美麗的湯解析一些HTML得到去除,但是我不斷收到這些問題:空間,同時使用beautifulsoup

html = """ <p><b>Influenza</b>, commonly known as "the <b>flu</b>", is an <a href="/wiki/Infectious_disease" title="Infectious disease" class="mw-redirect">infectious disease</a> of <a href="/wiki/Bird" title="Bird">birds</a> and <a href="/wiki/Mammal" title="Mammal">mammals</a> caused by <a href="/wiki/RNA_virus" title="RNA virus">RNA viruses</a> of the family <a href="/wiki/Orthomyxoviridae" title="Orthomyxoviridae">Orthomyxoviridae</a>, the <a href="/wiki/Orthomyxoviridae#Types_of_influenza_virus" title="Orthomyxoviridae">influenza viruses</a>. The most common <a href="/wiki/Symptom" title="Symptom">symptoms</a> are <a href="/wiki/Chills" title="Chills">chills</a>, <a href="/wiki/Fever" title="Fever">fever</a>, <a href="/wiki/Rhinorrhea" title="Rhinorrhea">runny nose</a>, <a href="/wiki/Sore_throat" title="Sore throat">sore throat</a>, <a href="/wiki/Myalgia" title="Myalgia">muscle pains</a>, <a href="/wiki/Headache" title="Headache">headache</a> (often severe), <a href="/wiki/Cough" title="Cough">coughing</a>, weakness/<a href="/wiki/Fatigue_(medical)" title="Fatigue (medical)">fatigue</a> and <a href="/wiki/Malaise" title="Malaise">general discomfort</a>. Although it is often confused with other <a href="/wiki/Influenza-like_illness" title="Influenza-like illness">influenza-like illnesses</a>, especially the <a href="/wiki/Common_cold" title="Common cold">common cold</a>, influenza is a more severe disease caused by a different type of virus.<sup id="cite_ref-Eccles_1-0" class="reference"><a href="#cite_note-Eccles-1"><span>[</span>1<span>]</span></a></sup> Influenza may produce <a href="/wiki/Nausea" title="Nausea">nausea</a> and <a href="/wiki/Vomiting" title="Vomiting">vomiting</a>, particularly in children,<sup id="cite_ref-Merck_2-0" class="reference"><a href="#cite_note-Merck-2"><span>[</span>2<span>]</span></a></sup> but these symptoms are more common in the unrelated <a href="/wiki/Gastroenteritis" title="Gastroenteritis">gastroenteritis</a>, which is sometimes inaccurately referred to as "stomach flu" or "24-hour flu".<sup id="cite_ref-3" class="reference"><a href="#cite_note-3"><span>[</span>3<span>]</span></a></sup></p>""" 

爲HTML是在湯類,所以我打印文本與html.text

我得到這個:

Influenza, commonly known as "theflu", is aninfectious diseaseofbirdsandmammalscaused byRNA virusesof the familyOrthomyxoviridae, theinfluenza viruses. The most commonsymptomsarechills,fever,runny nose,sore throat,muscle pains,headache(often severe),coughing, weakness/fatigueandgeneral discomfort. Although it is often confused with otherinfluenza-like illnesses, especially thecommon cold, influenza is a more severe disease caused by a different type of virus.[1]Influenza may producenauseaandvomiting, particularly in children,[2]but these symptoms are more common in the unrelatedgastroenteritis, which is sometimes inaccurately referred to as "stomach flu" or "24-hour flu".[3] 

正如你所看到的,很多空間移除。爲什麼會發生這種情況,我該如何預防它?

回答

1

這裏的第三和第四BeautifulSoup版本之間的區別:

>>> from bs4 import BeautifulSoup as BeautifulSoup4 
>>> from BeautifulSoup import BeautifulSoup as BeautifulSoup3 
>>> BeautifulSoup4(html).text 
u'Influenza, commonly known as "the flu", is an infectious disease of birds and mammals caused by RNA viruses of the family Orthomyxoviridae, the influenza viruses. The most common symptoms are chills, fever, runny nose, sore throat, muscle pains, headache (often severe), coughing, weakness/fatigue and general discomfort. Although it is often confused with other influenza-like illnesses, especially the common cold, influenza is a more severe disease caused by a different type of virus.[1] Influenza may produce nausea and vomiting, particularly in children,[2] but these symptoms are more common in the unrelated gastroenteritis, which is sometimes inaccurately referred to as "stomach flu" or "24-hour flu".[3]' 
>>> BeautifulSoup3(html).text 
u'Influenza, commonly known as "theflu", is aninfectious diseaseofbirdsandmammalscaused byRNA virusesof the familyOrthomyxoviridae, theinfluenza viruses. The most commonsymptomsarechills,fever,runny nose,sore throat,muscle pains,headache(often severe),coughing, weakness/fatigueandgeneral discomfort. Although it is often confused with otherinfluenza-like illnesses, especially thecommon cold, influenza is a more severe disease caused by a different type of virus.[1]Influenza may producenauseaandvomiting, particularly in children,[2]but these symptoms are more common in the unrelatedgastroenteritis, which is sometimes inaccurately referred to as "stomach flu" or "24-hour flu".[3]' 

所以,一個方法來解決它是於swtich到bs4

美麗的湯3不再被 開發,並建議所有新的 項目的美麗湯4。