Python是否将html插入mysql?

Python是否将html插入mysql?,python,mysql,Python,Mysql,如何通过Python将html插入mysql sql_data = "INSERT INTO `index_table`.`tab_data` (`id`, `content`, `copyfrom`) VALUES ( NULL, '%s','0')" 我想插入的html代码是这样的 <p><img src='http://img.domain.com/topic/image/20150731/db9475df317519db7622c25f23a42b0130700277

如何通过Python将html插入mysql

sql_data = "INSERT INTO `index_table`.`tab_data` (`id`, `content`, `copyfrom`) VALUES ( NULL, '%s','0')"
我想插入的html代码是这样的

<p><img src='http://img.domain.com/topic/image/20150731/db9475df317519db7622c25f23a42b0130700277.jpg' /></p>

我的python代码的图片

content = "<p><img src='http://img.domain.com/topic/image/20150731/db9475df317519db7622c25f23a42b0130700277.jpg' /></p>" 
tx.execute(sql_data % (tx.commit(str(content))) ) 
class MysqlPipeline(object):  
def __init__(self):                              
    self.dbpool = adbapi.ConnectionPool('MySQLdb', 
            host = 'localhost', 
            db = 'index',  
            user = 'name',  
            passwd = 'pass',  
            cursorclass = MySQLdb.cursors.DictCursor,  
            charset = 'utf8',  
            use_unicode = False
    )  

# pipeline dafault function                      
def process_item(self, item, spider):  
    query = self.dbpool.runInteraction(self._conditional_insert, item)  
    return item  


# insert the data to databases                    
def _conditional_insert(self, tx, item): 
    now_data = str(time.strftime('%Y%m%d',time.localtime(time.time())))  

    sql_data = ('INSERT INTO `index`.`news_data` (`id`, `content`, `maxcharperpage`, `allow_comment`) VALUES ( LAST_INSERT_ID(), "%s", "10000", "1")')
    try:
        thumbname = item['images'][0]['path']
        title = str(item['title'][0]) 
        picid = item['catid']
        image_locals = "http://img.domain.com/topic/image/"+now_data+"/"+thumbname
        #print maxid
        localimgs = list()
        for img in item['images']:
            imgsrc = img['path']
            #localimg = str("<p><img src='http://img.domain.com/topic/image/"+now_data+"/"+imgsrc+"' /></p>").replace('full/','')
            localimg = imgsrc
            localimgs.append(localimg)
        neirong = ''.join(localimgs)# neirong is a str object, html code.
        print neirong

        tx.execute(sql_data , [neirong]) # Is this line right? but it doesn't work

    except MySQLdb.Error, e:
        print "Error %d: %s" % (e.args[0], e.args[1])
    return item
content=“

” tx.execute(sql_数据%(tx.commit(str(content)))
但它不起作用

下面是更多的python代码

content = "<p><img src='http://img.domain.com/topic/image/20150731/db9475df317519db7622c25f23a42b0130700277.jpg' /></p>" 
tx.execute(sql_data % (tx.commit(str(content))) ) 
class MysqlPipeline(object):  
def __init__(self):                              
    self.dbpool = adbapi.ConnectionPool('MySQLdb', 
            host = 'localhost', 
            db = 'index',  
            user = 'name',  
            passwd = 'pass',  
            cursorclass = MySQLdb.cursors.DictCursor,  
            charset = 'utf8',  
            use_unicode = False
    )  

# pipeline dafault function                      
def process_item(self, item, spider):  
    query = self.dbpool.runInteraction(self._conditional_insert, item)  
    return item  


# insert the data to databases                    
def _conditional_insert(self, tx, item): 
    now_data = str(time.strftime('%Y%m%d',time.localtime(time.time())))  

    sql_data = ('INSERT INTO `index`.`news_data` (`id`, `content`, `maxcharperpage`, `allow_comment`) VALUES ( LAST_INSERT_ID(), "%s", "10000", "1")')
    try:
        thumbname = item['images'][0]['path']
        title = str(item['title'][0]) 
        picid = item['catid']
        image_locals = "http://img.domain.com/topic/image/"+now_data+"/"+thumbname
        #print maxid
        localimgs = list()
        for img in item['images']:
            imgsrc = img['path']
            #localimg = str("<p><img src='http://img.domain.com/topic/image/"+now_data+"/"+imgsrc+"' /></p>").replace('full/','')
            localimg = imgsrc
            localimgs.append(localimg)
        neirong = ''.join(localimgs)# neirong is a str object, html code.
        print neirong

        tx.execute(sql_data , [neirong]) # Is this line right? but it doesn't work

    except MySQLdb.Error, e:
        print "Error %d: %s" % (e.args[0], e.args[1])
    return item
类MysqlPipeline(对象):
定义初始化(自):
self.dbpool=adbapi.ConnectionPool('MySQLdb',
主机='localhost',
db='索引',
user='name',
passwd='pass',
cursorclass=MySQLdb.cursors.DictCursor,
字符集='utf8',
使用unicode=False
)  
#管道达法尔函数
def过程_项目(自身、项目、蜘蛛):
query=self.dbpool.runInteraction(self.\u conditional\u insert,item)
退货项目
#将数据插入数据库
定义条件插入(自身、发送、项目):
现在\u data=str(time.strftime(“%Y%m%d”,time.localtime(time.time()))
sql_data=('INSERT-INTO'index`.'news_-data`('id`、'content`、'maxcharperpage`、'allow_-comment`)值(最后一个_-INSERT_-id(),“%s”,“10000”,“1”))
尝试:
thumbname=项目['images'][0]['path']
title=str(项目['title'][0])
picid=项目['catid']
图像_局部变量=”http://img.domain.com/topic/image/“+now_data+”/“+thumbname”
#打印最大ID
localimgs=list()
对于项目['images']中的img:
imgsrc=img['path']
#localimg=str(“

”)。替换('full/','') 局部MG=imgsrc localimgs.append(localimg) neirong=''.join(localimgs)#neirong是一个str对象,html代码。 印内容 tx.execute(sql_数据,[neirong])这行对吗?但它不起作用 除了MySQLdb.Error,e: 打印“错误%d:%s”%(e.args[0],e.args[1]) 退货项目

如果
tx
是游标对象,请将
content
作为参数传递,而不是自己构建sql。否则,你应该考虑引用自己。(防止SQL注入)


tx是一个游标对象,而不是connection@jianbingMa,我相应地删除了一条注释。错误1064:您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以了解在'>@jianbingMa附近使用的正确语法,您是否传递了答案代码中显示的参数
tx.execute(sql_数据,[content])
?我已经尝试过了,但仍然不起作用。我添加了更多有问题的python代码,请看一看