【发布时间】:2010-12-20 23:25:28
【问题描述】:
_mysql_exceptions.Warning: Incorrect string value: '\xE7\xB9\x81\xE9\xAB\x94...' for column 'html' at row 1
def getSource(theurl, moved = 0):
if moved == 1:
theurl = urllib2.urlopen(theurl).geturl()
urlReq = urllib2.Request(theurl)
urlReq.add_header('User-Agent',random.choice(agents))
urlResponse = urllib2.urlopen(urlReq)
htmlSource = urlResponse.read()
return htmlSource
new_u = Url(source_url = source_url, source_url_short = source_url_short, source_url_hash = source_url_hash, html = htmlSource)
new_u.save()
为什么会这样? 我基本上是在下载页面的 URL...然后使用 Django 将其保存到数据库中。
它只是有时会发生......有时它工作正常。
编辑:好像我必须将数据库设置为 UTF-8?执行此操作的命令是什么?
【问题讨论】:
-
执行查询的代码请...
-
鲍比,查询是 new_u, new_u.save() 。这是 Django 查询
-
@alex:哦……从来没有使用过那个系统。最好的猜测是,您没有转义 html-String,而他正试图插入“错误”值。对于 PHP,该功能称为 mysql_real_escape_string。
标签: python mysql html database django