我编写了一个简单的脚本来获取最热门的 300 条包含特定主题标签的推文。
for self._tweet in tweepy.Cursor(self._api.search,q=self._screen_name,count=300, lang="en").items(300):
self._csvWriter.writerow([self._tweet.created_at, self._tweet.text.encode('utf-8')])
Run Code Online (Sandbox Code Playgroud)
它运行良好,并将结果保存到 CSV,但推文被截断。
我这样修改代码,添加twitter_mode=extended参数:
for self._tweet in tweepy.Cursor(self._api.search,q=self._screen_name,count=300, lang="en", tweet_mode="extended").items(300):
self._csvWriter.writerow([self._tweet.created_at, self._tweet.text.encode('utf-8')])
Run Code Online (Sandbox Code Playgroud)
但我得到了这个例外:
AttributeError: 'Status' object has no attribute 'text
Run Code Online (Sandbox Code Playgroud)
我的问题是:如何使用光标保存完整的推文?(完整=未截断)
提前致谢(抱歉,我是一个 Tweepy 新手,试图尽可能多地学习)
我写了一个简单的方法来将字典写入 CSV。
它运行良好,但我想知道是否可以提高速度(在我的测试中编写 1000 行的 CSV 需要 6 秒)。
我的问题是:如何提高这段代码的速度?(如果可能的话)
预先感谢您的帮助。
def fast_writer(self, f_name, text_dict):
try:
start = timer()
# Windows
if os.name == "nt":
with open(f_name, 'w', newline='') as self._csv_file:
self._writer = csv.writer(self._csv_file)
for self._key, self._value in text_dict.items():
self._writer.writerow([self._key, self._value])
# Unix/Linux
else:
with open(f_name, 'w') as self._csv_file:
self._writer = csv.writer(self._csv_file)
for self._key, self._value in text_dict.items():
self._writer.writerow([self._key, self._value])
end = timer()
print("[FastWriter_time] ", end - start)
except BaseException:
print("[ERROR] Unable to write file on disk. Exit...")
sys.exit()
Run Code Online (Sandbox Code Playgroud)