鱼C论坛

 找回密码
 立即注册
查看: 3254|回复: 4

python导入数据库时报错

[复制链接]
发表于 2016-1-13 18:16:11 | 显示全部楼层 |阅读模式
4鱼币
from BeautifulSoup import *
import pymssql
import dbi, odbc  
import time
import re
import urllib2
import random



url = 'http://www.xmcourt.gov.cn/ygsf/cpws/mssp/'
def open_url(url):
   
    user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko'  
    headers = { 'User-Agent' : user_agent }
    req = urllib2.Request(url,headers=headers)
    response = urllib2.urlopen(req)
    html = response.read().decode('utf-8')
   
    return html



def getFromPatten(patten,src,single=False):  
    rst = "";  
    p = re.compile(patten,re.S)  
    all = p.findall(src)  
    for matcher in all:  
        rst += matcher + " "  
        if( single ):  
            break  
    return rst.strip()


def get_info():
    '''
    for i in range(2,269):
    j = i-1
        if j ==0:
            url = 'http://www.xmcourt.gov.cn/ygsf/sdgg/index.htm?currpage=1'
        else:
            url = 'http://www.xmcourt.gov.cn/ygsf/sdgg/index_'+str(j)+'.htm?currpage='+str(i)
    '''
    url = 'http://www.xmcourt.gov.cn/ygsf/cpws/mssp/'
    html = open_url(url)
    soup = BeautifulSoup(html)
   
    a = soup.findAll('li')
   
   
    for i in a:
        j = getFromPatten(r'a href=".(.+?)"',str(i),True)
        
        link = 'http://www.xmcourt.gov.cn/ygsf/cpws/mssp'+j
        #link = 'http://www.xmcourt.gov.cn/ygsf/cpws/mssp/201512/t20151217_31591.htm'
        data = open_url(link)
        xinx = BeautifulSoup(data)
        
        title = xinx.head.title.string.encode('utf-8')
        
        nrf = getFromPatten(r'<DIV style.+?</DIV>',data,True)
        dr = re.compile(r'<[^>]+>',re.S)
        nr = dr.sub('',nrf)
        anhao = getFromPatten(u"<DIV style.+?裁.+?15pt; '>(.+?)</div>",data,True)
        
      
        
        
        fbtime = getFromPatten(u'发布时间:(.+?)&nbsp',data,True)

        laiy = getFromPatten(u'文字来源:(.+?)&nbsp',data,True)
        
        info = [(anhao,title,fbtime,laiy,nr)]
        
        print fbtime
        
        conn = pymssql.connect(host='192.168.0.123', user='sa', password='glad2015', database='test',charset="utf8")
        curson = conn.cursor()
        sql = 'insert into text2(案号,标题,发布时间,来源,内容) values(%s,%s,%s,%s,%s)'

         
        curson.executemany(sql,info)
        conn.commit()
        curson.close()
        conn.close()


Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\XJkt.py", line 92, in <module>
    print(get_info())
  File "C:\Users\Administrator\Desktop\XJkt.py", line 85, in get_info
    curson.executemany(sql,info)
  File "pymssql.pyx", line 473, in pymssql.Cursor.executemany (pymssql.c:7465)
  File "pymssql.pyx", line 447, in pymssql.Cursor.execute (pymssql.c:6867)
  File "_mssql.pyx", line 998, in _mssql.MSSQLConnection.execute_query (_mssql.c:10765)
  File "_mssql.pyx", line 1029, in _mssql.MSSQLConnection.execute_query (_mssql.c:10639)
  File "_mssql.pyx", line 1149, in _mssql.MSSQLConnection.format_and_run_query (_mssql.c:11786)
  File "_mssql.pyx", line 200, in _mssql.ensure_bytes (_mssql.c:2582)
  File "D:\python27\lib\encodings\utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe6 in position 42: invalid continuation byte


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-1-13 18:28:24 | 显示全部楼层
看样子是连接MySQL出错了?
你数据库的字符集是UTF-8的吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-1-14 08:43:43 | 显示全部楼层
hldh214 发表于 2016-1-13 18:28
看样子是连接MySQL出错了?
你数据库的字符集是UTF-8的吗?

解决了,把字段名改成英文就好
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-1-18 01:18:02 | 显示全部楼层
解决了金币给我呗,没金币啊,不过不论搞什么最好都不要用中文字符。英文最好,符号其次。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-5-18 20:12:04 | 显示全部楼层
舒方 发表于 2016-1-14 08:43
解决了,把字段名改成英文就好

请问 你是学了多久学到这种程度的
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-19 06:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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