美文网首页
python 报错

python 报错

作者: clshinem | 来源:发表于2017-04-13 19:59 被阅读0次

报错如下:global name 'score' is not defined
代码如下:

for item in soup.findAll('div', {'class': 'comment-item'}):
        print i
        f=open('/users/xuye/desktop/json1/json{}.json'.format(j),'a')
        global f
        for comment in item.findAll('p'):
            global desc
            desc = comment.text 
            # print(desc)
             # get comment

        for rating in item.findAll('span', {'class': 'rating'}):
            global score
            score = str(int(rating['class'][0][7:]) / 10)
            # print(score)
        date = {
            'score':score,
            'comment':desc
        }

全局变量没有定义;在循环外定义一个score变量就解决了

score = ''
    for item in soup.findAll('div', {'class': 'comment-item'}):
        print i
        ·
        ·
        ·

相关文章

网友评论

      本文标题:python 报错

      本文链接:https://www.haomeiwen.com/subject/mehiattx.html