### youtubeの特定の動画ページ内のユーザーネーム、コメント、いいね数を取得して それをpandasで形式を整えてcsv保存をしようとしたのですが デフォルトのutf8だと文字化けが発生したのでshift_jisで保存しようとしたら 下記の様なエラーメッセージが出てきました Traceback (most recent call last): File "<pyshell#23>", line 1, in <module> a.to_csv(r"C:\Users\ユーザー\Desktop\test.csv", encoding="shift_jis") File "C:\Users\ユーザー\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\generic.py", line 3902, in to_csv return DataFrameRenderer(formatter).to_csv( File "C:\Users\ユーザー\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\formats\format.py", line 1152, in to_csv csv_formatter.save() File "C:\Users\ユーザー\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\formats\csvs.py", line 266, in save self._save() File "C:\Users\ユーザー\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\formats\csvs.py", line 271, in _save self._save_body() File "C:\Users\ユーザー\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\formats\csvs.py", line 309, in _save_body self._save_chunk(start_i, end_i) File "C:\Users\ユーザー\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\formats\csvs.py", line 320, in _save_chunk libwriters.write_csv_rows( File "writers.pyx", line 75, in pandas._libs.writers.write_csv_rows UnicodeEncodeError: 'shift_jis' codec can't encode character '\ufe0f' in position 37: illegal multibyte sequence 翻訳サイトで翻訳した所どうも不正なマルチバイトシーケンスとかいうのがあるらしく保存が出来ないみたいです 作ったコードは下記になります
python
import time import pandas as pd import pyautogui as pgui import random import datetime as dt import sys from selenium import webdriver from selenium.webdriver.support.ui import Select from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.chrome import service from selenium.webdriver.common.alert import Alert from selenium.webdriver.support import expected_conditions as EC mypage = "https://www.youtube.com/watch?v=7UXnj_vWtgY" def login(): global driver driver_path = "c:/driver/chromedriver.exe" driver = webdriver.Chrome(driver_path) driver.maximize_window() driver.get(mypage) time.sleep(4) driver.execute_script("window.scrollTo(0,480)") time.sleep(5) def comment_gather(): comment_list = {} #コメント欄の情報を取得 section = driver.find_elements_by_css_selector("div[id='contents'][class=' style-scope ytd-item-section-renderer style-scope ytd-item-section-renderer']") comment_section = section[0].find_elements_by_css_selector("ytd-comment-thread-renderer[class='style-scope ytd-item-section-renderer']") #コメント欄から一つずつ抜いていく for n, one in enumerate(comment_section): #ユーザーネームを取得 user = one.find_element_by_css_selector("span[class=' style-scope ytd-comment-renderer style-scope ytd-comment-renderer']") user = user.text comment_list[n] = [user] #コメントを取得 comment = one.find_element_by_css_selector("yt-formatted-string[id='content-text']") comment = comment.text comment_list[n].append(comment) #コメント評価数を取得 nice = one.find_element_by_css_selector("span[id='vote-count-middle'][class='style-scope ytd-comment-action-buttons-renderer']") nice = nice.text comment_list[n].append(nice) #ネームとコメントを表示 print(user, "\n\n", comment) print("\n\n\n\n\n") df = pd.DataFrame.from_dict(comment_list, orient="index", columns=["名前", "コメント", "いいね数"]) return df
このコードのlogin()を実行した後にcomment_gather()で名前、コメント、いいね数を取得して
その後
a.to_csv(r"C:\Users\ユーザー\Desktop\test.csv", encoding="shift_jis")
を実行すると
上記の様なエラーが出てきました
解決策を教えていただけないでしょうか
追記
encoding=cp932というのも試してみましたがshift_jisと同じくエラーが出ます
回答1件
あなたの回答
tips
プレビュー