2016-10-05 10 views
1

私は<script>タグとbeatifulsoupを利用してタグ内のコンテンツを取り除こうとしています。私はドキュメントに行って、本当に簡単な関数呼び出しのようです。この機能の詳細はhereです。ここで私はこれまでに解析されているHTMLページのコンテンツがある...Beautifulsoup decompose()

<body class="pb-theme-normal pb-full-fluid"> 
    <div class="pub_300x250 pub_300x250m pub_728x90 text-ad textAd text_ad text_ads text-ads text-ad-links" id="wp-adb-c" style="width: 1px !important; 
    height: 1px !important; 
    position: absolute !important; 
    left: -10000px !important; 
    top: -1000px !important; 
    "> 
</div> 
<div id="pb-f-a"> 
</div> 
    <div class="" id="pb-root"> 
    <script> 
    (function(a){ 
     TWP=window.TWP||{}; 
     TWP.Features=TWP.Features||{}; 
     TWP.Features.Page=TWP.Features.Page||{}; 
     TWP.Features.Page.PostRecommends={}; 
     TWP.Features.Page.PostRecommends.url="https://recommendation-hybrid.wpdigital.net/hybrid/hybrid-filter/hybrid.json?callback\x3d?"; 
     TWP.Features.Page.PostRecommends.trackUrl="https://recommendation-hybrid.wpdigital.net/hybrid/hybrid-filter/tracker.json?callback\x3d?"; 
     TWP.Features.Page.PostRecommends.profileUrl="https://usersegment.wpdigital.net/usersegments"; 
     TWP.Features.Page.PostRecommends.canonicalUrl="" 
    })(jQuery); 

    </script> 
    </div> 
</body> 

は、あなたがそのようないくつかのWebコンテンツを持っていて、BeautifulSoupオブジェクトにsoup_htmlと呼ばれることを持っている想像してみてください。私がsoup_html.script.decompose()を実行し、オブジェクトsoup_htmlをまだそこに置いているとします。 <script>とそのタグ内のコンテンツをどのように取り除くことができますか?

markup = 'The html above' 
soup = BeautifulSoup(markup) 
html_body = soup.body 

soup.script.decompose() 

html_body 
+0

実行中の実際のコードを貼り付けます。あなたが説明したステップをテストすると、すべてが問題なくなります。 **編集**また、閉じた 'div'がありませんでしたが、BSの問題はありません – 4140tm

答えて

2

soup.script.decompose()

これは、 "スープ" のみから単一のスクリプト要素を削除します。代わりに、私はあなたがそれらのすべて分解するためのものだと思う:ここでは、alecxeが提供する答えに詳述すると

for script in soup("script"): 
    script.decompose() 
+1

何らかの理由で' decompose() 'が動作しなくなりました。今、 '.txt'ファイルに' script'コードがあります。私はドキュメンテーションに行きましたが、指示は前とほぼ同じです。 – Gilbert

+0

「JSとCSSを削除する」 スープスクリプト( 'script'、 'style'): script.decompose() open(my_params ['q'] + '_' + str(count)+ 'ファイル' + 'my_params [' q '] +' _ '+ str(count)+' .txt( '.txt'、 'w')としてwebpage_out: webpage_out.write(soup.get_text()) '+'は正常に作成されました)。 count + = 1 を除く: pass''' – Gilbert

0

をフルスクリプトは誰の参考のためにある:

selects = soup.findAll('select') 
for match in selects: 
    match.decompose() 
0

soup.script.decompose ()は、html_body変数ではなく、スープ変数からのみ削除します。それをhtml_body変数からも削除する必要があります。 (私は思う。)エラーが

0

私は次のコードで問題を解決することができました...

scripts = soup.findAll(['script', 'style']) 
    for match in scripts: 
     match.decompose() 
     file_content = soup.get_text() 
     # Striping 'ascii' code 
     content = re.sub(r'[^\x00-\x7f]', r' ', file_content) 
    # Creating 'txt' files 
    with open(my_params['q'] + '_' + str(count) + '.txt', 'w+') as webpage_out: 
     webpage_out.write(content) 
     print('The file ' + my_params['q'] + '_' + str(count) + '.txt ' + 'has been created successfully.') 
     count += 1 

with open(...が一部であったということでしたかfor match...

コードがやったことではありません作業...

scripts = soup.findAll(['script', 'style']) 
    for match in scripts: 
     match.decompose() 
     file_content = soup.get_text() 
     # Striping 'ascii' code 
     content = re.sub(r'[^\x00-\x7f]', r' ', file_content) 
     # Creating 'txt' files 
     with open(my_params['q'] + '_' + str(count) + '.txt', 'w+') as webpage_out: 
      webpage_out.write(content) 
      print('The file ' + my_params['q'] + '_' + str(count) + '.txt ' + 'has been created successfully.') 
      count += 1