鱼C论坛

 找回密码
 立即注册
查看: 1896|回复: 2

[已解决]python- MySQLdb 这个传输为什么会报错?

[复制链接]
发表于 2016-10-13 09:01:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
py的代码:



# -*- coding:utf-8 -*-
import urllib2,re,MySQLdb

urla = 'http://www.quanshu.net/book/9/9055/'  #这个是盗墓笔记

def getlist():
        html = urllib2.urlopen(urla).read()
        html = html.decode('gb2312').encode('utf-8')
        reg = re.compile(r'<li><a href="(.*?)" title="(.*?)">(.*?)</a>')
        urls = re.findall(reg,html)
        return urls


def getcontent(url):
        html = urllib2.urlopen(url).read()
        html = html.decode('gbk').encode('utf-8')
        reg = re.compile(r'style5\(\);</script>(.*?)<script type="text/javascript">')
        content = re.findall(reg,html)
        return content

class Sql(object):
        conn = MySQLdb.connect(
                host = 'localhost',
                port = 3306,
                user = 'root',
                passwd = 'passwd',
                db = "xiaoshuo",
                charset = "utf8",
        )
        
               
        def adddata(self,title,content):
                cur = self.conn.cursor()
                cur.execute("insert into books values(NULL,'%s','%s')" %(title,content))
                cur.close()
                self.conn.commit()
mysql = Sql()


for i in getlist():
        print '正在爬去%s' %i[1]
        title = i[1]
        content = getcontent(urla + i[0])
        print '正在插入数据库 %s' %i[1]
        mysql.adddata(title,content)
        break
       



报错:



Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
==== RESTART: C:\Users\Administrator\Desktop\shujuwajue\全书网小说爬取.py ====
正在爬去国庆贺文,非盗墓笔记,免费奉送。,共6035字
正在插入数据库 国庆贺文,非盗墓笔记,免费奉送。,共6035字

Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\shujuwajue\全书网小说爬取.py", line 45, in <module>
    mysql.adddata(title,content)
  File "C:\Users\Administrator\Desktop\shujuwajue\全书网小说爬取.py", line 34, in adddata
    cur.execute("insert into books values(NULL,'%s','%s')" %(title,content))
  File "build\bdist.win-amd64\egg\MySQLdb\cursors.py", line 205, in execute
    self.errorhandler(self, exc, value)
  File "build\bdist.win-amd64\egg\MySQLdb\connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ';&nbsp;&nbsp;&nbsp;\\xe6\\x88\\x91\\xe6\\x8e\\xa5\\xe4\\xb8\\x8b\\xe6\\x9d\\xa5\\xe8\\xa6\\x81\\' at line 1")
>>>










最佳答案
2016-11-3 16:11:01
import urllib2,re,MySQLdb 之后 加上这个试试
reload(sys)
sys.setdefaultencoding('utf8')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-10-13 09:02:07 | 显示全部楼层
本帖最后由 Mr.Young 于 2016-10-13 09:03 编辑

求指导      
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-11-3 16:11:01 | 显示全部楼层    本楼为最佳答案   
import urllib2,re,MySQLdb 之后 加上这个试试
reload(sys)
sys.setdefaultencoding('utf8')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-3 19:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表