我找到下一个代码,通过使用它,我可以从组中获取文本注释。随着vk api没有问题,一切都很好。但是在excel中打印有问题:
import vk_requests
import time
import xlsxwriter
import datetime
import codecs
api = vk_requests.create_api(app_id='',
login='',
password='',
phone_number='')
Group_id = 1
Topic_id = 2
#первое цифровое значение по ссылке - группа, второе - обсуждение
a = api.board.getComments(group_id=Group_id, topic_id=Topic_id)
CommentsCount = a['count']
print('N = ',CommentsCount)
num = 10 # input(':\n')
count = num
def GetTopicInfo(Offset):
r = api.board.getComments(group_id=Group_id,
topic_id=Topic_id,
count=num,
offset=Offset)
return r
# Create a workbook and add a worksheet.
'''
workbook = xlsxwriter.Workbook('data.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write('A1', 'Hello world')
workbook.close()
'''
workbook = xlsxwriter.Workbook('unicode_data.xlsx',{'strings_to_urls': False})
worksheet = workbook.add_worksheet()
bold = workbook.add_format({'bold': True})
#Создание колонок
worksheet.write(0, 0, u'Date', bold)
worksheet.write(0, 1, u'Author', bold)
worksheet.write(0, 2, u'id', bold)
worksheet.write(0, 3, u'Text', bold)
Offset = 0
row = 1
col = 0
while Offset < CommentsCount:
r = GetTopicInfo(Offset)
i = 0
print(Offset)
try:
while i < num:
Topic_info = r['items'][i]
Topic_info_from = r['items'][i]['from_id']
Topic_info_id = r['items'][i]['id']
Topic_info_text = r['items'][i]['text']
Topic_info_date = datetime.datetime.fromtimestamp(
int(r['items'][i]['date'])
).strftime('%Y-%m-%d %H:%M:%S')
worksheet.write(row, col, Topic_info_date)
worksheet.write(row, col + 1, Topic_info_from)
worksheet.write(row, col + 2, Topic_info_id)
worksheet.write(row, col + 3, Topic_info_text)
row += 1
print(i, Topic_info)
i = i + 1
except IndexError:
print(u'Конец')
Offset = Offset + num
time.sleep(.35)
workbook.close()
我有这个错误:
Traceback (most recent call last):
File "vk.py", line 90, in <module>
print(i, Topic_info)
File "C:\Program Files\Anaconda3\lib\encodings\cp866.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u0306' in position
497: character maps to <undefined>
Exception ignored in: <bound method Workbook.__del__ of <xlsxwriter.workbook.Wor
kbook object at 0x00000064530D6518>>
Traceback (most recent call last):
File "C:\Program Files\Anaconda3\lib\site-packages\xlsxwriter\workbook.py", li
ne 148, in __del__
Exception: Exception caught in workbook destructor. Explicit close() may be requ
ired for workbook.
评论中的代码效果很好,但是在运算符中没有。我需要重写什么才能解析?