注意するのは、キャラクタセットの指定です。 ※ Windows では、指定しない場合は SHIFT_JIS で出力されます0003.py ( utf8n )
#! /usr/bin/env python3.1 import urllib.request # ********************************************************** # ※ エラーをファイルに出力しています # ※ file=object は、object に write メソッドが必要です # ※ python312.chm::/library/functions.html#open # # ※ SSL が有効です # ********************************************************** try: response = urllib.request.urlopen('https://mixi.jp/') except urllib.error.URLError as e: print(e,file=open("error.log","at")) exit() html = response.read() # サーバーからの http ヘッダ print( response.info() ) # ********************************************************** # ※ ログイン前のページをファイルに出力しています # # ※ python312.chm::/library/codecs.html#standard-encodings # ※ euc_jp | eucjp, ujis, u-jis # ※ mixi は、EUC-JP です # # ※ 出力される mixi_home.txt は、SHIFT_JIS になります # ********************************************************** print( html.decode("euc_jp"),file=open("mixi_home_sjis.txt","wt") ) # ********************************************************** # そのまま EUC-JP で出力 # ********************************************************** print( html.decode("euc_jp"), file=open( "mixi_home_ujis.txt", "wt", encoding="euc_jp" ) )
関連する記事 Python3.1 : URL を読み出す( bytes から string )
タグ:Python
|
【Pythonの最新記事】
- Python : Excel の新しいブックのデフォルトのシートのセルに直接値をセットして、オートフィルを Range オブジェクトから実行する
- Python : shift_jis の3列の csv フォーマットのデータを ttk.Treeview に表示する
- Python : ttk.Treeview で表形式を使用して環境変数の一覧表示
- XAMPP + Python( 3.8 ) で WEBアプリの基礎部分構築 : その ( 1 )
- XAMPP + Python( 3.8 ) で WEBアプリの基礎部分構築 : その ( 4 ) : リダイレクトと関数とログ出力
- XAMPP + Python( 3.8 ) で WEBアプリの基礎部分構築 : その ( 3 ) : cgi.FieldStorage() から ディクショナリ
- XAMPP + Python( 3.8 ) でWEBアプリの基礎部分構築 : その ( 2 ) : QUERY_STRING と 画面定義
- Python + MySQL + IFRAME + Bootstrap : 問い合せ WEB アプリテンプレート
- MySQL Connector/Python の使用方法概要のまとめ
- Python3 : 言語的デザインの特徴と要点
- Python ドキュメントに沿った、テキストファイル読み込みの理解
- Python3 でメール送信 ( さくらインターネット )
- Python 3.6 で GET/POST メソッドを想定した CGI 用の簡易テンプレートを作成してみました
- Eclipse + Python(Pydev) : pywin32(COM使用の為) + MySQL Connector/ODBC でループ処理をしながら更新
- Eclipse + Python(Pydev) : MySQL Connector/Python でループ処理をしながら更新
- Pleiades Eclipse 4.7 Oxygen で Python を使って MySQL にアクセスする
- Python 3.4 : インストールと最初にする事( Windows )
- Python3.x : URL を読み出す( bytes から string )
- Python3.1 : URLを読み出して、バイナリのままファイル出力する
- Python3.1 : クッキーを保持して二つの URL にアクセスする