开发者

Getting error when INSERT into MySQL

_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.Reques开发者_如何学编程t(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()

Why is this happening? I am basically downloading URL of a page...and then saving it to a database using Django.

It only happens sometimes....and sometimes it works fine.

Edit: it seems like I have to set the database to UTF-8? What is the command to do that?


You basically need to ensure proper a string encoding. E.g. the string you provide to django is not UTF-8 encoded and therefore some characters can't be resolved.

Some helpful advice on how to find the encoding of the requested page can be found here: urllib2 read to Unicode


There are 2 ways to go if you want to alter the character set in MySQL. First is the default of the database, see MySQL Alter database, and the second is per-table: MySQL Alter Table.

The database gives the default charset for, I believe, new tables. This can be overridden on a per-table basis, which you need to do since you already have tables. "utf8" is a supported character set.

Also have a look at Blog about UTF8 with django and MySQL.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜