2016-12-26 16 views
0

私はHTMLタグ間のテキストを抽出する必要があります。私はBeautifulSoupを使ってデータを抽出し、テキストを変数に格納して後で処理しました。後で私は、私が抽出する必要があるテキストが2つの異なるタグに入っていることが分かった。ただし、テキストを抽出して同じ変数に格納する必要があることに注意してください。以前のコードとサンプルのHTMLテキスト情報が提供されています。最終結果、つまり期待される結果を得る方法を教えてください。Pythonスープを使ってダイナミックHTMLタグ間にテキストを抽出します

サンプルHTMLテキスト:

<DIV CLASS="c0"><P CLASS="c1"><SPAN CLASS="c2">1 of 80 DOCUMENTS</SPAN></P> 
<DIV CLASS="c0"><BR><P CLASS="c1"><SPAN CLASS="c2">Financial Times (London, England)</SPAN></P> 
<DIV CLASS="c0"><BR><P CLASS="c1"><SPAN CLASS="c2">Copyright 2015 The Financial Times Ltd.<BR>All Rights Reserved<BR>Please do not cut and paste FT articles and redistribute by email or post to the web.</SPAN></P> 

<DIV CLASS="c0"><P CLASS="c1"><SPAN CLASS="c2">80 of 80 DOCUMENTS</SPAN></P> 
</DIV> 
<BR><DIV CLASS="c3"><P CLASS="c1"><SPAN CLASS="c2">Financial Times (London,England)</SPAN></P> 
</DIV> 
<DIV CLASS="c3"><P CLASS="c1"><SPAN CLASS="c2">Copyright 1990 The Financial Times Limited</SPAN></P> 
</DIV> 

上記HTMLテキストから、私は単一の変数への文書(80の文書、80件の文書の80の1)を格納する必要があります。他のテキストと同様に、同様のアプローチに従います。 div.c0のコードを書きました

 soup = BeautifulSoup(response, 'html.parser') 
     docpublicationcpyright = soup.select('div.c0') 

     list1 = [b.text.strip() for b in docpublicationcpyright] 
     doccountvalues = list1[0:len(list1):3] 
     publicationvalues = list1[1:len(list1):3] 
     copyrightvalues = list1[2:len(list1):3] 
     documentcount = doccountvalues 

     publicationpaper = publicationvalues 

ご協力いただけると助かります。

+0

誰もこれで私を助けることができる以下のように見える3つのすべての必要なフィールドを抽出することができ、質問&に投稿された唯一のサンプルHTML

soup = BeautifulSoup(response, 'html.parser') documentElements = soup.find_all('span', text=re.compile(r'of [0-9]+ DOCUMENTS')) documentCountList = [] publicationPaperList = [] documentPublicationCopyrightList = [] for elem in documentElements: documentCountList.append(elem.get_text().strip()) if elem.parent.find_next_sibling('div'): publicationPaperList.append(elem.parent.find_next_sibling('div').find('span').get_text().strip()) documentPublicationCopyrightList.append(elem.parent.find_next_sibling('div').find_all('span')[1].get_text()) else: publicationPaperList.append(elem.parent.parent.find_next('div').get_text().strip()) documentPublicationCopyrightList.append(elem.parent.parent.find_next('div').find_next('div').get_text().strip()) print(documentCountList) print(publicationPaperList) print(documentPublicationCopyrightList) 

を考慮し、サンプルコードを書きました – Mho

+0

出力サンプルを投稿します。 –

+0

サンプル出力:documentcount = [ドキュメント80件中1件、ドキュメント80件中80件]、publicationpaper = [Financial Times(London、England)、Financial Times(London、England)] – Mho

答えて

1

与えられたサンプルHTMLが適切に構造化されていません。たとえば、最初のDIV要素の終了タグがありません。とにかくこのタイプのHTMLでも正規表現を使用すると、必要なデータをスクラップできます。

私は出力が

[u'1 of 80 DOCUMENTS', u'80 of 80 DOCUMENTS'] 
[u'Financial Times (London, England)', u'Financial Times (London,England)'] 
[u'Copyright 2015 The Financial Times Ltd.All Rights ReservedPlease do not cut and paste FT articles and redistribute by email or post to the web.', u'Copyright 1990 The Financial Times Limited'] 
+0

ありがとうございます。それは正常に働いた。 – Mho

関連する問題