百度云语义分析+mongodb:try-except需要continue

# -*- coding: UTF-8 -*-
_author_ = 'zy'
_date_ = '2018/12/12 0012 14:41'
import json ,requests,urllib,pymongo,time
API_KEY=
SEC_KEY=
def get_access_token():
    """
    获取百度AI平台的Access Token
    """
    host.format(APIK=API_KEY,SECK=SEC_KEY)
    print(host)
    request = urllib.request.Request(host)
    request.add_header('Content-Type', 'application/json; charset=UTF-8')
    response = urllib.request.urlopen(request)
    content = response.read().decode('utf-8')
    rdata = json.loads(content)
    print(rdata['access_token'])
    return rdata['access_token']

def sentiment_classify(text):
    """
    获取文本的感情偏向(消极 or 积极 or 中立)
    参数:
    text:str 本文
    """
    raw = {"text":"内容"}
    raw['text'] = text
    data = json.dumps(raw).encode('utf-8')
   # AT = get_access_token()#"Access Token"
 
    host = "https://aip.baidubce.com/rpc/2.0/nlp/v1/sentiment_classify?charset=UTF-8&access_token="+AT
    request = urllib.request.Request(url=host,data=data)
    request.add_header('Content-Type', 'application/json')
    response = urllib.request.urlopen(request)
    content = response.read().decode('utf-8')
    rdata = json.loads(content)
    #print(rdata['items'])#0:负向,1:中性,2:正向['items'][0]['sentiment']

    return rdata

if __name__=='__main__':
    #get_access_token()
    #result=sentiment_classify('武汉大学')
    dbname='comment1213'
    #sentiment_analysis
    #positive_prob
    #confidence
    #negative_prob
    #sentiment_value
    client = pymongo.MongoClient('127.0.0.1', 27017)  # 缺少一步骤进行属性的清洗操作,确定是否有这个值
    db = client.weibo
    cursor = db[dbname].find({'sentiment':{'$exists':False}})  # segmentation#
    for i in cursor:
        text=i['text']
        try:
            if text != None:
                result=sentiment_classify(text)
                sentimentcode=result['items'][0]['sentiment']
                if sentimentcode==1:
                    sentiment='中性'
                if sentimentcode==0:
                    sentiment = '消极'
                if sentimentcode==2:
                    sentiment = '积极'
                data={
                    'positive_prob':result['items'][0]['positive_prob'],
                    'confidence': result['items'][0]['confidence'],
                    'negative_prob': result['items'][0]['negative_prob'],
                    'sentiment': sentiment
                }
                mongoid = i['_id']
                myquery = {"_id": mongoid}
                newvalues = {"$set": data}
                db[dbname].update_one(myquery, newvalues)
                print('插入一个')
        except KeyError:
            time.sleep(1)
        continue
# except KeyError:

你可能感兴趣的:(python)