###前提・実現したいこと
Python3.5, BeautifulSoup4, selenium, PhantomJSを使ってbodyタグ内のテキストをすべて取得するような処理をしたのですが、JavascriptやCSSのソースコードも一緒に取得してしまいます。それらは必要ないので、テキストのみで取得、または取得後の処理でテキストのみにできる方法がありましたら教えてください。
取得する際の主なソースは以下の通りです。
python
1driver.get(url) 2html = driver.page_source 3soup = BeautifulSoup(html, 'lxml') 4html_new = soup.prettify() 5soup = BeautifulSoup(html_new, 'lxml') 6 7all_text += soup.body.get_text()
いらないモノはこんな感じです
javascript
1 window._pt_lt = new Date().getTime(); 2 window._pt_sp_2 = []; 3 _pt_sp_2.push('setAccount,7954951e'); 4 var _protocol = (("https:" == document.location.protocol) ? " https://" : " http://"); 5 (function() { 6 var atag = document.createElement('script'); atag.type = 'text/javascript'; atag.async = true; 7 atag.src = _protocol + 'js.ptengine.jp/pta.js'; 8 var stag = document.createElement('script'); stag.type = 'text/javascript'; stag.async = true; 9 stag.src = _protocol + 'js.ptengine.jp/pts.js'; 10 var s = document.getElementsByTagName('script')[0]; 11 s.parentNode.insertBefore(atag, s);s.parentNode.insertBefore(stag, s); 12 })(); 13 14
CSS
1{float:none;clear:none;margin:0;padding:0;border:0 solid;border-radius:0;font-size:100%;font-weight:normal;font-style:normal;text-indent:0;letter-spacing:0;word-spacing:0;text-transform:none;vertical-align:baseline;}#uftMain div{background-image:url(https://s.yimg.jp/yui/jp/uft/2.0.1/img/uftChnk.png);background-repeat:no-repeat;}
あなたの回答
tips
プレビュー