| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

差分

ナビゲーションに移動 検索に移動
284 バイト追加 、 2020年9月18日 (金) 14:14
from bs4 import BeautifulSoup # To get everything
==解析==
*===文字列およびファイルハンドルによる文書解析===
soup = BeautifulSoup(open("index.html"))
soup = BeautifulSoup("<html>data</html>")
*===URLを指定して解析===
*[[https://docs.python.org/ja/2.7/library/urllib2.html urllib2 モジュールは、Python 3 で urllib.request, urllib.error に分割されました。]]
<pre>
from bs4 import BeautifulSoup
soup = BeautifulSoup(urllib.request.urlopen('http://xxxxx.com'))
</pre>
===エンコードの変換===
*文字化けする場合(例えばSHIFT-JIS)の対処
<pre>
response = urllib.request.urlopen(url)
html = response.read().decode(response.headers.get_content_charset(), errors='ignore')
parsed_html = BeautifulSoup(html, 'html.parser')
</pre>

案内メニュー