我使用Beautiful Soup来解析一些HTML,但是我一直遇到这些问题:
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]
如您所见,许多空格被删除。为什么会发生这种情况?如何防止它?
答案 0 :(得分:1)
以下是第3版和第4版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]'
因此,解决问题的一种方法是切换到bs4:
美丽的汤3不再存在 开发,并推荐所有新的美丽汤4 项目