Python使用Mysql过程中一些错误

时间:2023-03-09 09:56:01
Python使用Mysql过程中一些错误

Python使用Mysql过程中一些错误

ssh登录远程服务器

ssh ubuntu@xxx.xxx.xx.xx

第一:ubuntu终端中登录Mysql

mysql -uroot -p

然后输入密码即可在本地终端中登录mysql

第一:Mysql ERROR 1045 (28000): Access denied for user 'root'@'localhost'

这是ubuntu中安装Mysql过程没有设置密码,导致无法登录

mysql -uroot -p

use mysql;

update user set password=password("123456") where user="root";

flush privileges;

exit

第二:Mysqldb的sql语句转义

Python爬虫过程中如果像网页中插入html会提示sql错误,我们需要对sql语句进行转义,这样就很方便的把sql语句进行转义:

import MySQLdb

sql = MySQLdb.escape_string(sql)

但是转义后可以存入数据库,那么怎么取出html文本呢

	 //需要安装HTMLParser
sudo pip install HTMLParser html_parser = HTMLParser.HTMLParser()
html_content = html_parser.unescape(html_content)

参考: