插数据库报错
(1366, "Incorrect string value: '\\xF3\\xB0\\x84\\xBC</...' for column 'content' at row 1")
改编码utf8mb4解决
#!/usr/bin/env python # coding:utf8 # author:Z time:2018/7/25
import requests from bs4 import BeautifulSoup import pymysql url='http://www.5bite.com/post/5234.html' html=requests.get(url) soup=BeautifulSoup(html.text,'lxml') content=soup.select('body > div.inner.container > main > article > div.entry') connect = pymysql.Connect( host='127.0.0.1', port=3306, user='root', passwd='', db='abc', charset='utf8' ) cursor = connect.cursor() cursor.execute("SET names 'utf8mb4'")#utf8mb4 # 插入数据 sql2 = """ INSERT INTO aa (content) VALUES ('%s') """ data = tuple(content) try : if True: cursor.execute(sql2 % data) connect.commit() print('成功插入cnstock 1 条数据') cursor.close() connect.close() else: print('已有数据') cursor.close() connect.close() except Exception as e: print(e)
详细解释
https://www.cnblogs.com/z-x-y/p/9365626.html