Я использую Beautiful Soup для некоторых документов HTML. Все, что я хочу, это извлечь текст без каких-либо тегов.
from bs4 import BeautifulSoup
def aex_extract_text(html):
soup = BeautifulSoup(html, 'lxml')
text = soup.get_text()
return text
Мой вклад несколько патологичен в количестве избыточных тегов. Но это автоматически генерируемый HTML, поэтому не должно быть никаких пропущенных углов или чего-либо подобного. Это какая-то система Drupal, которая создает HTML. Почти на всех документах он работает нормально, но примерно на 1/50 я получаю первые несколько хороших строк, а затем пробелы между каждым символом, и теги не удаляются.
Кто-нибудь знает, что вызывает такого рода поломки? Я не вижу ничего очевидного. Факт, что это вспыхивает в середине параграфа, сбивает с толку. Мне не нужно идеальное сохранение текста, поэтому, если мне нужно вытащить несколько символов, я не возражаю.
Пример ввода:
[...]
</FONT></FONT></P> <P LANG=en-US CLASS=western ALIGN=JUSTIFY STYLE=margin-bottom: 0cm; line-height: 150%> <BR> </P> <P LANG=en-US
CLASS=western ALIGN=JUSTIFY STYLE=margin-bottom: 0cm; line-height: 150%> <FONT FACE=Century Gothic, serif><FONT SIZE=3>The 2</FONT></FONT><SUP><FONT
FACE=Century Gothic, serif><FONT SIZE=3>nd</FONT></FONT></SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3> Respondent was declared the duly
elected Member of Parliament for the Chilanga Constituency.</FONT></FONT></P> <P LANG=en-US CLASS=western ALIGN=JUSTIFY STYLE=margin-bottom:
0cm; line-height: 150%> <BR> </P> <P LANG=en-US CLASS=western ALIGN=JUSTIFY STYLE=margin-bottom: 0cm; line-height: 150%> <FONT FACE=Century
Gothic, serif><FONT SIZE=3>The result was unsuccessfully challenged, in the High Court, to have it declared null and void on a number of allegations
not relevant to this appeal as it will be seen later in this judgment.</FONT></FONT></P> <P LANG=en-US CLASS=western ALIGN=JUSTIFY STYLE=margin-
bottom: 0cm; line-height: 150%> <BR> </P> <P LANG=en-US CLASS=western ALIGN=JUSTIFY STYLE=margin-bottom: 0cm; line-height: 150%> <FONT FACE=Century
Gothic, serif><FONT SIZE=3>It is also not in dispute that the learned trial judge informed the parties of his intention to call some witnesses
after the defence had closed its case. Five witnesses, in total, were called by the court and they were designated as court witnesses.</FONT></FONT></P> <P LANG=en-US CLASS=western ALIGN=JUSTIFY STYLE=margin-bottom:
0cm; line-height: 150%> <BR> </P> <P LANG=en-US CLASS=western ALIGN=JUSTIFY STYLE=margin-bottom: 0cm; line-height: 150%> <FONT FACE=Century Gothic, serif><FONT SIZE=3>This appeal is premised on one ground of appeal
which is attacking the learned trial judge’s jurisdiction to call witnesses on his own motion.
[...]
Пример вывода:
[...]
Второй ответчик был объявлен должным образом избранным членом парламента от округа Чиланга. В Высоком суде был безуспешно оспорен результат признания его недействительным по ряду обвинений, не имеющих отношения к этой апелляции, как будет видно позже в этом решении. Также не оспаривается, что ученый судья первой инстанции проинформировал стороны о своем намерении вызвать некоторых свидетелей из-под стражи в суде. Находится в режиме реального времени, в режиме реального времени, в режиме реального времени. / F O N T> / F O N T> / P> P L A N G = e n - U S C L A S S = w s t e r n A L I G N = J U S T I F Y S T Y L E = m r g i n - b o t t m: 0 c m; l i n e - h i i h h t: 1 5 0%> B R> / P> P L A N G = e - U S C L A S S = w s A s I L I G N = J U S T I F Y S T Y L E = m r r g i n - b to t o m:
[...]
UPDATE:
вот полный ввод до того, где он идет не так:
<!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.0 Transitional//EN> <HTML> <HEAD> <META HTTP-EQUIV=CONTENT-TYPE CONTENT=text/html; charset=windows-1252> <TITLE></TITLE> <META NAME=GENERATOR CONTENT=OpenOffice.org 3.3 (Win32)> <META NAME=AUTHOR CONTENT=user> <META NAME=CREATED CONTENT=20081007;9280000> <META NAME=CHANGED CONTENT=20081007;9280000> <STYLE TYPE=text/css> <!-- @page { size: 21.59cm 27.94cm; margin-left: 2.54cm; margin-right: 2.54cm; margin-top: 1.27cm; margin-bottom: 2.54cm } P { margin-bottom: 0.21cm; line-height: 115%; text-align: left } P.western { font-family: Calibri, serif; font-size: 11pt; so-language: en-US } P.cjk { font-family: Arial Unicode MS, sans-serif; so-language: en-US } P.ctl { font-family: Calibri, serif; font-size: 11pt } --> </STYLE> </HEAD> <BODY LANG=en-ZA DIR=LTR STYLE=border: none; padding: 0cm> <DIV TYPE=HEADER> <P LANG=en-US ALIGN=CENTER STYLE=margin-bottom: 0cm; line-height: 0.18cm> <SPAN STYLE=font-style: normal><SPAN STYLE=font-weight: normal>J <SDFIELD TYPE=PAGE SUBTYPE=RANDOM FORMAT=ARABIC>26</SDFIELD></SPAN></SPAN></P> <P LANG=en-US STYLE=margin-bottom: 1.17cm; line-height: 0.18cm><BR> </P> </DIV> <P LANG=en-US CLASS=western ALIGN=RIGHT STYLE=margin-bottom: 0.35cm; line-height: 150%> <FONT FACE=Calibri, serif><FONT SIZE=2 STYLE=font-size: 11pt><SPAN LANG=en-US><SPAN STYLE=font-style: normal><SPAN STYLE=font-weight: normal> (706)</SPAN></SPAN></SPAN></FONT></FONT></P> <P LANG=en-US CLASS=western STYLE=margin-bottom: 0cm; line-height: 150%> <BR> </P> <P LANG=en-US CLASS=western STYLE=margin-bottom: 0cm; line-height: 150%> <FONT FACE=Century Gothic, serif><FONT SIZE=3><B>IN THE SUPREME COURT OF ZAMBIA SCZ APPEAL NO.182 OF 2007 </B></FONT></FONT></P> <P LANG=en-US CLASS=western STYLE=margin-bottom: 0.35cm><A NAME=DDE_LINK></A> <FONT FACE=Century Gothic, serif><FONT SIZE=3><B>HOLDEN AT LUSAKA SCZ Judgment No. 31 OF 2008</B></FONT></FONT></P> <P LANG=en-US CLASS=western STYLE=margin-bottom: 0.35cm><FONT FACE=Century Gothic, serif><FONT SIZE=3><B>(CIVIL JURISDICTION)</B></FONT></FONT></P> <P LANG=en-US CLASS=western STYLE=margin-bottom: 0cm; line-height: 150%> <BR> </P> <P LANG=en-US CLASS=western STYLE=margin-bottom: 0cm; line-height: 150%> <FONT FACE=Century Gothic, serif><FONT SIZE=3><B>IN THE MATTER OF: THE ELECTORAL ACT NO.12 OF 2006 SECTION 93 OF THE </B></FONT></FONT> </P> <P LANG=en-US CLASS=western STYLE=margin-left: 3.81cm; text-indent: 1.27cm; margin-bottom: 0cm; line-height: 150%> <FONT FACE=Century Gothic, serif><FONT SIZE=3><B>LAWS OF ZAMBIA</B></FONT></FONT></P> <P LANG=en-US CLASS=western STYLE=margin-left: 5.08cm; text-indent: -5.08cm; margin-bottom: 0cm; line-height: 150%> <FONT FACE=Century Gothic, serif><FONT SIZE=3><B>IN THE MATTER OF: PARLIAMENTARY ELECTION FOR CHILANGA CONSTITUENCY HELD ON THE 28</B></FONT></FONT><SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3><B>TH</B></FONT></FONT></SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3><B> SEPTEMBER, 2006</B></FONT></FONT></P> <P LANG=en-US CLASS=western STYLE=margin-bottom: 0cm; line-height: 150%> <FONT FACE=Century Gothic, serif><FONT SIZE=3><B>IN THE MATTER OF: AN ELECTION PETITION </B></FONT></FONT> </P> <P LANG=en-US CLASS=western STYLE=margin-bottom: 0cm; line-height: 150%> <BR> </P> <P LANG=en-US CLASS=western STYLE=margin-bottom: 0cm; line-height: 150%> <FONT FACE=Century Gothic, serif><FONT SIZE=3><B>BETWEEN:</B></FONT></FONT></P> <P LANG=en-US CLASS=western STYLE=margin-bottom: 0.35cm><FONT FACE=Century Gothic, serif><FONT SIZE=3><B> PRISCILLA MWENYA KAMANGA - APPELLANT</B></FONT></FONT></P> <P LANG=en-US CLASS=western STYLE=margin-bottom: 0.35cm><FONT FACE=Century Gothic, serif><FONT SIZE=3><B> AND</B></FONT></FONT></P> <P LANG=en-US CLASS=western STYLE=margin-bottom: 0.35cm><FONT FACE=Century Gothic, serif><FONT SIZE=3><B> THE ATTORNEY-GENERAL - 1</B></FONT></FONT><SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3><B>ST</B></FONT></FONT></SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3><B> RESPONDENT</B></FONT></FONT></P> <P LANG=en-US CLASS=western STYLE=margin-bottom: 0cm; line-height: 150%> <BR> </P> <P LANG=en-US CLASS=western STYLE=margin-bottom: 0cm; line-height: 150%> <FONT FACE=Century Gothic, serif><FONT SIZE=3><B> HON. NG’ANDU PETER MAGANDE - 2</B></FONT></FONT><SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3><B>ND</B></FONT></FONT></SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3><B> RESPONDENT</B></FONT></FONT></P> <P LANG=en-US CLASS=western STYLE=margin-bottom: 0cm; line-height: 150%> <BR> </P> <P LANG=en-US CLASS=western STYLE=margin-bottom: 0cm; line-height: 150%> <FONT FACE=Century Gothic, serif><FONT SIZE=3><B>CORAM : Sakala, CJ.,Chirwa, Mumba, Chitengi and Mushabati; JJS.</B></FONT></FONT></P> <P LANG=en-US CLASS=western STYLE=margin-bottom: 0.35cm><FONT FACE=Century Gothic, serif><FONT SIZE=3><B>On 25</B></FONT></FONT><SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3><B>TH</B></FONT></FONT></SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3><B> June, 2006 and 19</B></FONT></FONT><SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3><B>th</B></FONT></FONT></SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3><B> August, 2008</B></FONT></FONT></P> <P LANG=en-US CLASS=western STYLE=margin-bottom: 0.35cm><FONT FACE=Century Gothic, serif><FONT SIZE=3><B>For the Appellants : Mr.B.C. Mutale, SC. and Mr L. Kalaluka of Ellis and Company</B></FONT></FONT></P> <P LANG=en-US CLASS=western STYLE=margin-bottom: 0cm; border-top: none; border-bottom: 1.50pt solid #00000a; border-left: none; border-right: none; padding-top: 0cm; padding-bottom: 0.04cm; padding-left: 0cm; padding-right: 0cm; line-height: 150%> <FONT FACE=Century Gothic, serif><FONT SIZE=3><B>For the 1</B></FONT></FONT><SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3><B>st</B></FONT></FONT></SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3><B> Respondent: Mr M. Mukwasa – State Advocate</B></FONT></FONT></P> <P LANG=en-US CLASS=western STYLE=margin-bottom: 0.35cm><FONT FACE=Century Gothic, serif><FONT SIZE=3><B>For the 2</B></FONT></FONT><SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3><B>nd</B></FONT></FONT></SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3><B> Respondent: Mr S.C. Malama, SC. of Jaques and Partners</B></FONT></FONT></P> <P LANG=en-US CLASS=western STYLE=margin-bottom: 0.35cm><FONT FACE=Century Gothic, serif><FONT SIZE=3>______________________________________________________________________________</FONT></FONT></P> <P LANG=en-US CLASS=western ALIGN=CENTER STYLE=margin-bottom: 0cm; border-top: none; border-bottom: 1.50pt solid #00000a; border-left: none; border-right: none; padding-top: 0cm; padding-bottom: 0.04cm; padding-left: 0cm; padding-right: 0cm; line-height: 150%> <FONT FACE=Century Gothic, serif><FONT SIZE=4><B>JUDGMENT</B></FONT></FONT></P> <P LANG=en-US CLASS=western STYLE=margin-bottom: 0cm; line-height: 150%> <BR> </P> <P LANG=en-US CLASS=western ALIGN=JUSTIFY STYLE=margin-bottom: 0cm; line-height: 150%> <FONT FACE=Century Gothic, serif><FONT SIZE=3><B>Mushabati, JS., delivered the judgment of the Court.</B></FONT></FONT></P> <P LANG=en-US CLASS=western ALIGN=JUSTIFY STYLE=margin-bottom: 0cm; line-height: 150%> <BR> </P> <P LANG=en-US CLASS=western STYLE=margin-bottom: 0.35cm><BR><BR> </P> <P LANG=en-US CLASS=western ALIGN=RIGHT STYLE=margin-bottom: 0cm; line-height: 150%> <FONT FACE=Century Gothic, serif><FONT SIZE=3><B>(707)</B></FONT></FONT></P> <P LANG=en-US CLASS=western ALIGN=JUSTIFY STYLE=margin-bottom: 0cm; line-height: 150%> <FONT FACE=Century Gothic, serif><FONT SIZE=3><B>Cases referred to:</B></FONT></FONT></P> <OL> <LI><P LANG=en-US ALIGN=JUSTIFY STYLE=margin-bottom: 0cm; line-height: 150%> <FONT FACE=Calibri, serif><FONT SIZE=2 STYLE=font-size: 11pt><SPAN LANG=en-US><SPAN STYLE=font-style: normal><SPAN STYLE=font-weight: normal>Double Mwale Vs The People [1984] Z.R. 76</SPAN></SPAN></SPAN></FONT></FONT></P> </OL> <P LANG=en-US STYLE=margin-left: 1.27cm; margin-bottom: 0.35cm><FONT FACE=Century Gothic, serif><B>Simwanza Vs The People [1985] Z.R.15</B></FONT></P> <P LANG=en-US STYLE=margin-left: 1.27cm; margin-bottom: 0.35cm><FONT FACE=Century Gothic, serif><B>Jones Vs National Coal Board [1957] 2. ALL E.R. 155</B></FONT></P> <P LANG=en-US STYLE=margin-left: 1.27cm; margin-bottom: 0.35cm><FONT FACE=Century Gothic, serif><B>Re Enock and Zaretzky, Bock and Co. [1910] 1 K.B.327</B></FONT></P> <P LANG=en-US STYLE=margin-left: 1.27cm; margin-bottom: 0.35cm><FONT FACE=Century Gothic, serif><B>Porter Vs Magill [2002] 1 ALL E.R. 465</B></FONT></P> <P LANG=en-US STYLE=margin-left: 1.27cm; margin-bottom: 0.35cm><FONT FACE=Century Gothic, serif><B>Fallow Vs Calvert [1960] 2.Q.B. 201</B></FONT></P> <P LANG=en-US STYLE=margin-left: 1.27cm; margin-bottom: 0.35cm><FONT FACE=Century Gothic, serif><B>Water Welles Ltd Vs Jackson [1984] Z.R. 98</B></FONT></P> <P LANG=en-US STYLE=margin-left: 1.27cm; margin-bottom: 0.35cm><FONT FACE=Century Gothic, serif><B>Zambia Telecommunications Ltd Vs Celtel (Z) Ltd SCZ No. 90 of 2006 (unreported)</B></FONT></P> <P LANG=en-US STYLE=margin-left: 1.27cm; margin-bottom: 0.35cm><FONT FACE=Century Gothic, serif><B>Mazoka and others Vs Mwanawasa and others [2005] Z.R. 138</B></FONT></P> <P LANG=en-US STYLE=margin-left: 1.27cm; margin-bottom: 0.35cm><FONT FACE=Century Gothic, serif><B>Thomas Mumba and others Vs The People SCZ Appeal No. 92-95 (unreported) </B></FONT> </P> <P LANG=en-US STYLE=margin-left: 1.27cm; margin-bottom: 0.35cm><FONT FACE=Century Gothic, serif><B>Lewanika and others Vs Chiluba [1998] Z.R. 79</B></FONT></P> <P LANG=en-US STYLE=margin-left: 1.27cm; margin-bottom: 0.35cm><FONT FACE=Century Gothic, serif><B>Mabenga Vs Wina [2003] Z.R. 110</B></FONT></P> <P LANG=en-US ALIGN=JUSTIFY STYLE=margin-left: 1.27cm; margin-bottom: 0cm; line-height: 150%> <BR> </P> <P LANG=en-US CLASS=western ALIGN=JUSTIFY STYLE=margin-bottom: 0cm; line-height: 150%> <FONT FACE=Century Gothic, serif><FONT SIZE=3><B>Legislation referred to:</B></FONT></FONT></P> <P LANG=en-US CLASS=western STYLE=margin-bottom: 0.35cm><FONT FACE=Century Gothic, serif><B>Criminal Procedure Code, Cap. 87 – S.149</B></FONT></P> <P LANG=en-US CLASS=western STYLE=margin-bottom: 0.35cm><FONT FACE=Century Gothic, serif><B>Electoral Act, No.12 of 2006 – SS.102(3) and 103(1)(a) and (b)</B></FONT></P> <P LANG=en-US CLASS=western ALIGN=JUSTIFY STYLE=margin-bottom: 0cm; line-height: 150%> <BR> </P> <P LANG=en-US CLASS=western ALIGN=JUSTIFY STYLE=margin-bottom: 0cm; line-height: 150%> <FONT FACE=Century Gothic, serif><FONT SIZE=3><B>Other works referred to:</B></FONT></FONT></P> <P LANG=en-US CLASS=western STYLE=margin-bottom: 0.35cm><FONT FACE=Century Gothic, serif><B>Halsburys Laws of England 4</B></FONT><SUP><FONT FACE=Century Gothic, serif><B>th</B></FONT></SUP><FONT FACE=Century Gothic, serif><B> Edition Vol. 17 Page 195 Paragraph 281</B></FONT></P> <P LANG=en-US CLASS=western ALIGN=JUSTIFY STYLE=margin-bottom: 0cm; line-height: 150%> <BR> </P> <P LANG=en-US CLASS=western ALIGN=JUSTIFY STYLE=margin-bottom: 0cm; line-height: 150%> <FONT FACE=Century Gothic, serif><FONT SIZE=3>This is an appeal against the High Court judgment of 30</FONT></FONT><SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3>th</FONT></FONT></SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3> July, 2007 dismissing the appellant’s petition against the election of the 2</FONT></FONT><SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3>nd</FONT></FONT></SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3> Respondent as a Member of Parliament for the Chilanga Parliamentary Constituency during the Presidential and Parliamentary General Elections held on 28</FONT></FONT><SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3>th</FONT></FONT></SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3> September, 2006, seeking to nullify the 2</FONT></FONT><SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3>nd</FONT></FONT></SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3> Respondent’s election on a number of malpractices as pleaded in the petition.</FONT></FONT></P> <P LANG=en-US CLASS=western ALIGN=JUSTIFY STYLE=margin-bottom: 0cm; line-height: 150%> <BR> </P> <P LANG=en-US CLASS=western ALIGN=RIGHT STYLE=margin-bottom: 0cm; line-height: 150%> <BR> </P> <P LANG=en-US CLASS=western ALIGN=RIGHT STYLE=margin-bottom: 0cm; line-height: 150%> <FONT FACE=Century Gothic, serif><FONT SIZE=3><B>(708)</B></FONT></FONT></P> <P LANG=en-US CLASS=western ALIGN=JUSTIFY STYLE=margin-bottom: 0cm; line-height: 150%> <BR> </P> <P LANG=en-US CLASS=western ALIGN=JUSTIFY STYLE=margin-bottom: 0cm; line-height: 150%> <FONT FACE=Century Gothic, serif><FONT SIZE=3>We wish to state that in the court below, the petition was prosecuted by two Petitioners but the 2</FONT></FONT><SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3>nd</FONT></FONT></SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3> Petitioner, Capt. Cosmas Moono, is not a party to this appeal. However, for clarity’s sake we shall simply refer to the appellant as the Petitioner and the 1</FONT></FONT><SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3>st</FONT></FONT></SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3> and 2</FONT></FONT><SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3>nd</FONT></FONT></SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3> Respondents as the 1</FONT></FONT><SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3>st</FONT></FONT></SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3> and 2</FONT></FONT><SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3>nd</FONT></FONT></SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3> Respondents respectively, the titles they held in the court below.</FONT></FONT></P> <P LANG=en-US CLASS=western ALIGN=JUSTIFY STYLE=margin-bottom: 0cm; line-height: 150%> <BR> </P> <P LANG=en-US CLASS=western ALIGN=JUSTIFY STYLE=margin-bottom: 0cm; line-height: 150%> <FONT FACE=Century Gothic, serif><FONT SIZE=3>The undisputed facts of this case are that both the Petitioner and the 2</FONT></FONT><SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3>nd</FONT></FONT></SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3> Respondent contested the Parliamentary General Elections held on 28</FONT></FONT><SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3>th</FONT></FONT></SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3> September, 2006 in the Chilanga Constituency. The Petitioner stood on the Patriotic Front (PF) ticket; while the 2</FONT></FONT><SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3>nd</FONT></FONT></SUP><FONT FACE=Century Gothic, serif><FONT SIZE=3> Respondent stood on the ticket of the Movement for Multiparty Democracy (MMD).
UPDATE:
Я проголосовал, чтобы закрыть свой вопрос. Ответ (который работает) здесь:
BeautifulSoup возвращает неожиданные лишние пробелы