-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.py
91 lines (70 loc) · 2.57 KB
/
writer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# -*- coding: UTF-8 -*-
import copy
import csv
import os
import sys
import traceback
def get_filepath(type, nickname):
"""获取结果文件路径"""
file_dir = os.path.split(
os.path.realpath(__file__))[0] + os.sep + 'weibo' + os.sep + nickname
if type == 'img' or type == 'video':
file_dir = file_dir + os.sep + type
if not os.path.isdir(file_dir):
os.makedirs(file_dir)
if type == 'img' or type == 'video':
return file_dir
file_path = file_dir + os.sep + nickname + '.' + type
return file_path
def write_log(since_date):
"""当程序因cookie过期停止运行时,将相关信息写入log.txt"""
file_dir = os.path.split(
os.path.realpath(__file__))[0] + os.sep + 'weibo' + os.sep
if not os.path.isdir(file_dir):
os.makedirs(file_dir)
file_path = file_dir + 'log.txt'
content = u'cookie已过期,从%s到今天的微博获取失败,请重新设置cookie\n' % since_date
with open(file_path, 'ab') as f:
f.write(content.encode(sys.stdout.encoding))
class Writer:
def __init__(self, config):
self.writer = CsvWriter(config)
def write_user(self, user):
self.writer.write_user(user)
def write_weibo(self, weibo):
self.writer.write_weibo(weibo)
class CsvWriter:
def __init__(self, config):
self.config = config
def write_user(self, user):
self.user = user
result_headers = [
'微博名称',
'地点',
'日期',
'一天发文数',
'与疫情相关',
'疫情相关占比'
]
if not self.config['filter']:
result_headers.insert(-1, '被转发微博原始图片url')
result_headers.insert(-1, '是否为原创微博')
with open(get_filepath('csv', self.user['nickname']),
'a',
encoding='utf-8-sig',
newline='') as f:
csv_writer = csv.writer(f)
csv_writer.writerows([result_headers])
def write_weibo(self, weibo):
"""将爬取的信息写入csv文件"""
result_data = []
for index in weibo:
result_data.append(index)
with open(get_filepath('csv', self.user['nickname']),
'a',
encoding='utf-8-sig',
newline='') as f:
csv_writer = csv.writer(f)
csv_writer.writerow(result_data)
print(u'%d条微博写入csv文件完毕,保存路径:' % len(weibo))
print(get_filepath('csv', self.user['nickname']))