実現したいこと
PythonのPyAutoGUI・Pyperclipライブラリを利用して、「あるサイトで出力されたページの内容をコピー・保存後、必要な情報のみをテキストファイルに保存しなおす」というプログラムを作成しています。
前提
「サイトから必要な情報をコピー・保存する」という段階は問題なく実行されているのですが、その後必要な情報のみを抜き出して保存しなおす処理を行おうとすると下記のようなデコードエラーが表示されてしまいます。
※5/22AM追記:
エラーは1つ目のファイル「01.txt」の時点で発生しており、該当ファイルの1行目の内容(検証時)は「Nu Html Checker」です。
なお、エンコーディングはメモ帳で確認するとUTF-8、コメント欄で教えていただいた方法(pythonのchardetライブラリ使用)で確認すると「MacRoman', 'confidence': 0.4850749971805571, 'language': ''}」となります。
※5/22PM追記:
コメント欄で教えていただいた通り、読み込むテキストファイルのエンコーディングを「ISO-8859-1」に変更したところ、エラーコードは表示されなくなりました。
ただ、今回作成しているプログラムは「ウェブページの内容を保存したテキストファイルを読み込み、条件(errorlist)に当てはまる行のみを取得して「output.txt」に保存する」ところまでが処理の流れになっているのですが、エラーメッセージが表示されなくなっても「output.txt」の中身は「 results.write(f'\nページ{base}\n')」で記入したページ番号以外空白のままです。
もしかしたら読み取るテキストファイルのエンコーディングがMacRomanだと配列「errorlist」の中身を読み取れないということなのでしょうか。
見当違いなことを伺ってしまっているかもしれませんが、教えていただければ幸いです。
※5/23AM追記:
コメント欄でご提案いただいたように、「list」と「errors」を比較する段階で、両者の内容をprintするよう処理を変更して確認してみました。
結果としては下記の通り、見た目上では抽出条件に合った内容が複数見つかりましたが、「output.txt」にはやはり反映されていませんでした。
(list): 「Showing results for https://teratail.com/」
(errors): 「Showing results for」
※5/25AM追記:
コメント欄で教えていただいた方法で「Showing results for...」の行のみ残してアスキーコードを調べてみましたが、文字化けしている上、テキストファイルに保存された結果が50万文字超になってしまいました。
下記、結果のはじめの1000文字程度のみ記載します。
line: ÐÏࡱá > þÿ þÿÿÿ Y ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿýÿÿÿ þÿÿÿ þÿÿÿþÿÿÿ
c: Ðc: 208c: Ïc: 207c: c: 17c: àc: 224c: ¡c: 161c: ±c: 177c: c: 26c: ác: 225c: c: 0c: c: 0c: c: 0c: c: 0c: c: 0c: c: 0c: c: 0c: c: 0c: c: 0c: c: 0c: c: 0c: c: 0c: c: 0c: c: 0c: c: 0c: c: 0c: >c: 62c: c: 0c: c: 3c: c: 0c: þc: 254c: ÿc: 255c: c: 9c: c: 0c: c: 6c: c: 0c: c: 0c: c: 0c: c: 0c: c: 0c: c: 0c: c: 0c: c: 0c: c: 0c: c: 0c: c: 0c: c: 2c: c: 0c: c: 0c: c: 0c: c: 1c: c: 0c: c: 0c: c: 0c: c: 0c: c: 0c: c: 0c: c: 0c: c: 0c: c: 16c: c: 0c: c: 0c: c: 2c: c: 0c: c: 0c: c: 0c: c: 1c: c: 0c: c: 0c: c: 0c: þc: 254c: ÿc: 255c: ÿc: 255c: ÿc: 255c: c: 0c: c: 0c: c: 0c: c: 0c: c: 0c: c: 0c: c: 0c: c: 0c: Yc: 89c: c: 0c: c: 0c: c: 0c: ÿc: 255c: ÿc: 255c: ÿc: 255c: ÿc: 255c: ÿc: 255c: ÿc: 255c: ÿc: 255c: ÿc: 255c: ÿc: 255c: ÿc: 255c: ÿc: 255c: ÿc: 255c: ÿc: 255c: ÿc: 255c: ÿc: 255c: ÿc: 255c: ÿc: 255c: ÿc: 255c: ÿc: 255c: ÿc: 255c: ÿc: 255c: ÿc: 255c: ÿc: 255c: ÿc: 255c: ÿc: 255c: ÿc: 255c: ÿc: 255c: ÿc: 255c: ÿc: 255c: ÿc: 255c: ÿc: 255c: ÿc: 255c:
発生している問題・エラーメッセージ
Traceback (most recent call last): File "auto-validator.py", line 79, in <module> lines = file.readlines() File "codecs.py", line 322, in decode UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 0: invalid continuation byte Traceback (most recent call last): File "auto-validator.py", line 79, in <module> lines = file.readlines() File "codecs.py", line 322, in decode UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 0: invalid continuation byte
該当のソースコード
python
1import pyautogui as pg 2import pyperclip as pc 3import playsound as ps 4import time 5import os 6import sys 7 8# 出力用フォルダの用意 9foldername = 'input' 10is_folder = os.path.isdir(foldername) 11if not is_folder: 12 os.mkdir(foldername) 13 14# コピーしたURLの分割 15urls = pc.paste() 16urlList = urls.splitlines() 17 18# Chromeの起動 19pg.hotkey('win', 'r'); 20pc.copy('chrome.exe') 21pg.hotkey('ctrl', 'v') 22pg.hotkey('enter') 23time.sleep(4) 24 25# URLの数だけループさせる 26for line in urlList: 27 pg.hotkey('ctrl', 'l'); 28 pc.copy(line) 29 time.sleep(0.5) 30 pg.hotkey('ctrl', 'v'); 31 time.sleep(0.5) 32 pg.hotkey('enter') 33 time.sleep(5) 34 pg.hotkey('ctrl', 'l') 35 pc.copy(""":void(window.location='https://validator.w3.org/check?uri=%27+encodeURIComponent(window.location))""") 36 time.sleep(0.5) 37 pg.write('javascript') 38 time.sleep(1) 39 pg.hotkey('ctrl', 'v') 40 time.sleep(0.5) 41 pg.hotkey('enter') 42 time.sleep(6) 43 if urlList.index(line) == 0: 44 time.sleep(3) 45 pg.hotkey('ctrl', 'a') 46 time.sleep(1) 47 pg.hotkey('ctrl', 'c') 48 time.sleep(1) 49 # テキストへの出力 50 i = 1 51 if i < 10: 52 filename = f'{foldername}/0{i}.txt' 53 else: 54 filename = f'{foldername}/{i}.txt' 55 is_file = os.path.isfile(filename) 56 while is_file: 57 i += 1 58 if i < 10: 59 filename = f'{foldername}/0{i}.txt' 60 else: 61 filename = f'{foldername}/{i}.txt' 62 is_file = os.path.isfile(filename) 63 f = open(filename, 'w', encoding='UTF-8') 64 restxt = pc.paste() 65 f.write(restxt) 66 time.sleep(3) 67 f.close() 68 time.sleep(2) 69pg.hotkey('alt', 'f4') 70 71results = open('output.txt', 'a', encoding='UTF-8') 72errorlist = ['Showing results for', 'Consider adding a lang attribute to ', 'Element title must not be empty', 'seen when expecting text or an end tag.', 'Unclosed element ', 'not allowed as child of element', 'element in scope but a * end tag seen', 'Duplicate ID ', 'The first occurrence of ID ', 'Duplicate attribute ', 'End tag * seen, but there were open elements', 'Stray end tag ', 'inside comment', 'End of file seen and there were open elements', 'not allowed on element ', 'is missing one or more of the following attributes', 'seen but an element of the same type was already open', 'Fatal Error', 'No space between attributes', 'Saw " when expecting an attribute name', 'is missing a required instance of child element ', 'Quote " in attribute name. Probable cause: Matching quote missing somewhere earlier', 'consecutive hyphens in a comment'] 73 74for filename in os.listdir('input'): 75 base, ext = os.path.splitext(filename) 76 if ext == '.txt': 77 with open(f'input\{filename}', 'r', encoding='UTF-8') as file: 78 results.write(f'\nページ{base}\n') 79 lines = file.readlines() 80 for idx, line in enumerate(lines): 81 for errors in errorlist: 82 if errors in line: 83 results.write(line) 84 results.write(lines[idx + 1]) 85 results.write(lines[idx + 2]) 86results.close() 87 88ps.playsound(r'C:\Windows\Media\tada.wav')
補足情報(FW/ツールのバージョンなど)
なお、OSはWindows 10、Chromeのバージョンはバージョン: 113.0.5672.127(Official Build) (64 ビット)です。
よろしくお願いします。

あなたの回答
tips
プレビュー