報錯如下: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
}
全局變量沒有定義;在循環(huán)外定義一個score變量就解決了
score = ''
for item in soup.findAll('div', {'class': 'comment-item'}):
print i
·
·
·