2016-07-09 6 views
-1

以下はhtmlファイルからの抜粋です。 HTMLParserを使用してテキスト部分のみを取得したいです。PythonでHTMLParserを使用して、次のHTML TEXTからテキストを取り出す方法は?

<html> 
    <div class="js-tweet-text-container"> 
     <p class="TweetTextSize js-tweet-text tweet-text" data-aria-label-part= 
     "0" lang="en"><a class="twitter-atreply pretty-link js-nav" 
     data-mentioned-user-id="119293693" dir="ltr" href= 
     "/attrc"><s>@</s><b><strong>attrc</strong></b></a> as soon as <a class= 
     "twitter-atreply pretty-link js-nav" data-mentioned-user-id="352507986" 
     dir="ltr" href="/PoppySeedPlehzr"><s>@</s><b>PoppySeedPlehzr</b></a> 
     gets some <a class="twitter-hashtag pretty-link js-nav" 
     data-query-source="hashtag_click" dir="ltr" href= 
     "/hashtag/osquery?src=hash"><s>#</s><b>osquery</b></a> tables going for 
     Windows, that would be a good first use case for us :)</p> 
    </div> 
</html> 

私は上から以下の出力をしたい:

@attrc as soon as poppyseedplehzr gets some osquery tables going for 
windows,that would be a good first use case for us. 
+3

を[正しくコードをフォーマット]にしてください(http://meta.stackexchange.com/a/22189/248777)、自分自身に質問してください。あなたの問題やその状況を事前に知らない人は、あなたが求めていることを理解できますか? – mklement0

+1

何を試しましたか?期待される出力とはどのように違いますか?あなたのコードを見てみましょう。 –

答えて

1

は、のではなく、BeautifulSoupを使用してみましょう。最初のは、それをインストールしてみましょう:

pip install beautifulsoup4 

そして、我々のコード:

from bs4 import BeautifulSoup 

html = """ 
<html> 
    <div class="js-tweet-text-container"> 
     <p class="TweetTextSize js-tweet-text tweet-text" data-aria-label-part= 
     "0" lang="en"><a class="twitter-atreply pretty-link js-nav" 
     data-mentioned-user-id="119293693" dir="ltr" href= 
     "/attrc"><s>@</s><b><strong>attrc</strong></b></a> as soon as <a class= 
     "twitter-atreply pretty-link js-nav" data-mentioned-user-id="352507986" 
     dir="ltr" href="/PoppySeedPlehzr"><s>@</s><b>PoppySeedPlehzr</b></a> 
     gets some <a class="twitter-hashtag pretty-link js-nav" 
     data-query-source="hashtag_click" dir="ltr" href= 
     "/hashtag/osquery?src=hash"><s>#</s><b>osquery</b></a> tables going for 
     Windows, that would be a good first use case for us :)</p> 
    </div> 
    <div class="js-tweet-text-container"> 
     <p class="TweetTextSize js-tweet-text tweet-text" data-aria-label-part= 
     "0" lang="en"><a class="twitter-atreply pretty-link js-nav" 
     data-mentioned-user-id="119293693" dir="ltr" href= 
     "/attrc"><s>@</s><b><strong>attrc</strong></b></a> as soon as <a class= 
     "twitter-atreply pretty-link js-nav" data-mentioned-user-id="352507986" 
     dir="ltr" href="/PoppySeedPlehzr"><s>@</s><b>PoppySeedPlehzr</b></a> 
     gets some <a class="twitter-hashtag pretty-link js-nav" 
     data-query-source="hashtag_click" dir="ltr" href= 
     "/hashtag/osquery?src=hash"><s>#</s><b>osquery</b></a> tables going for 
     Windows, that would be a good first use case for us :)</p> 
    </div> 
</html> 
""" 

soup = BeautifulSoup(html, 'html.parser') 
for tweet in soup.find_all('p', class_="tweet-text"): 
    print ' '.join([line.strip() for line in tweet.get_text().splitlines()]) 

これは私たちに与えます:

@attrc as soon as @PoppySeedPlehzr gets some #osquery tables going for Windows, that would be a good first use case for us :) 
@attrc as soon as @PoppySeedPlehzr gets some #osquery tables going for Windows, that would be a good first use case for us :) 
+1

私はBeautifulSoupの推奨を2番目にします。 –

関連する問題