python mysql html转义问题

版权声明:本文为博主huanbia:http://blog.csdn.net/huanbia原创文章,未经博主允许不得转载。 https://blog.csdn.net/huanbia/article/details/76498374

有时我们想要通过python将爬虫得到的html代码存入mysql,这个时候由于原html中有各种特殊符号,尤其是像单引号和双引号,影响存储,此时我们需将特殊字符进行转义,通常python相关的mysql包一般会带有escape_string方法,比如我用的pymysql

import pymysql
print(pymysql.escape_string("'ab'c"))

结果为:\’ab\’c

当然不仅仅pymysql包支持,mysqldb同时也支持

猜你喜欢

转载自blog.csdn.net/huanbia/article/details/76498374