私は以下の問題がありました:Windows-1251にエンコードされたサイトはありますか?私はあなたがロシア語で必要なデータを取得するためにBeautifulSoupライブラリを使用しています。テストではファイル内の値が記録され、正しく表示されます。次に、このデータをデータベースに記録しようとしましたが、このエラーが発生しました。Warning: (1366, "Incorrect string value: '\\x98\\xD0\\xBD\\xD1\\x82\\xD0...' for column 'description' at row 1")
データベースエンコーディングはutf-8です。pymysqlがデータベースにロシア語のテキストを正しく書きません
def get_html(url):
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
response = urllib.request.urlopen(req)
return response.read()
以下のコードは、データベースに値を書き込む方法を示しています:
def write_to_db(chars, desc):
conn = pymysql.connect(host='localhost',
port=3307,
user='****',
password='****',
db='****',
charset='utf8',
cursorclass=pymysql.cursors.DictCursor)
global _id
with conn.cursor() as cursor:
sql = "INSERT INTO `articles` (`id_cat`, `characteristics`, `description`) VALUES (%s, %s, %s)"
cursor.execute(sql, (_id, chars, desc))
conn.commit()
我々は次の文字データベースで取得終了で:Модель
どうすればこの問題を解決できますか?
なぜそれがトリックを行うのかについての説明は、 – jwenting