実現したいこと
旧PC(Win10から裏口でWin11 24H2)の調子が悪くなって
きましたので新PC(Win11 24H2)を購入しました。
そこで、スクレイピングとは定義が違うかもですが
Google検索結果をファイルに落としますプログラムを
旧PC→新PCにコピーしましても動きません。
bs4とかpipでインストール済みです。
発生している問題・分からないこと
プログラム実行時にエラーで止まってしまいます。
エラーメッセージ
error
1D:\ドキュメント\めざましじゃんけん>mezamashi.py 2Traceback (most recent call last): 3 File "D:\ドキュメント\めざましじゃんけん\mezamashi.py", line 17, in <module> 4 before = soup.encode('cp932', "ignore") 5 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 6 File "C:\Users\(ユーザー名)\AppData\Local\Programs\Python\Python312\Lib\site-packages\bs4\element.py", line 2339, in encode 7 u = self.decode(indent_level, encoding, formatter) 8 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 9 File "C:\Users\(ユーザー名)\AppData\Local\Programs\Python\Python312\Lib\site-packages\bs4\__init__.py", line 1141, in decode 10 return prefix + super(BeautifulSoup, self).decode( 11 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 12 File "C:\Users\(ユーザー名)\AppData\Local\Programs\Python\Python312\Lib\site-packages\bs4\element.py", line 2443, in decode 13 piece = self._indent_string( 14 ^^^^^^^^^^^^^^^^^^^^ 15 File "C:\Users\(ユーザー名)\AppData\Local\Programs\Python\Python312\Lib\site-packages\bs4\element.py", line 2528, in _indent_string 16 space_before = formatter.indent * indent_level 17 ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~ 18TypeError: can't multiply sequence by non-int of type 'str' 19
該当のソースコード
Python
1import re 2import codecs 3import requests, bs4, datetime 4from urllib.parse import quote 5 6now = datetime.datetime.now() 7kekka_2 = "" 8 9url = 'https://search.yahoo.co.jp/realtime/search?p=%E3%82%81%E3%81%96%E3%81%BE%E3%81%97%E3%83%86%E3%83%AC%E3%83%93+%E3%82%81%E3%81%96%E3%81%BE%E3%81%97%E3%81%98%E3%82%83%E3%82%93%E3%81%91%E3%82%93+%E3%80%901%E6%88%A6%E7%9B%AE%E3%80%91&ei=UTF-8&ifr=tl_sc' 10response = requests.get(url) 11response.text.replace('\U0001f19e','') 12response.text.replace('\xa0','') 13 14response.raise_for_status() 15soup = bs4.BeautifulSoup(response.text, "html.parser") 16 17before = soup.encode('cp932', "ignore") 18res_2 = before.decode('cp932') 19 20name = re.findall('【1戦目】.+?めざまし', res_2) 21name2 = name[0] 22name3 = name2.replace('【', '\n【') 23 24save_file = 'kekka_' + now.strftime('%Y%m%d') + '.txt' 25 26with codecs.open(save_file, 'w', 'utf-8') as f: 27 f.write(name3) 28f.close() 29
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
エラー箇所をコメントアウトしてみましたが
ダメでした。
補足
旧PCのバージョンが『3.12.2』であった為、新PCでも
同じバージョンを入れていますが、
旧PCでは
python -V
コマンドで
Python 3.12.2
と表示されるのに
新PCでは
python -V
コマンドで
Python
とだけ出てきます。

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