本地化&2.0
This commit is contained in:
@@ -69,30 +69,40 @@ async def main():
|
||||
print(f"Database {args.init_db} initialized successfully.")
|
||||
return # Exit the main function cleanly
|
||||
|
||||
crawler = None
|
||||
try:
|
||||
crawler = CrawlerFactory.create_crawler(platform=config.PLATFORM)
|
||||
await crawler.start()
|
||||
|
||||
|
||||
crawler = CrawlerFactory.create_crawler(platform=config.PLATFORM)
|
||||
await crawler.start()
|
||||
|
||||
# Generate wordcloud after crawling is complete
|
||||
# Only for JSON save mode
|
||||
if config.SAVE_DATA_OPTION == "json" and config.ENABLE_GET_WORDCLOUD:
|
||||
try:
|
||||
file_writer = AsyncFileWriter(
|
||||
platform=config.PLATFORM,
|
||||
crawler_type=crawler_type_var.get()
|
||||
)
|
||||
await file_writer.generate_wordcloud_from_comments()
|
||||
except Exception as e:
|
||||
print(f"Error generating wordcloud: {e}")
|
||||
# Generate wordcloud after crawling is complete
|
||||
# Only for JSON save mode
|
||||
if config.SAVE_DATA_OPTION == "json" and config.ENABLE_GET_WORDCLOUD:
|
||||
try:
|
||||
file_writer = AsyncFileWriter(
|
||||
platform=config.PLATFORM,
|
||||
crawler_type=crawler_type_var.get()
|
||||
)
|
||||
await file_writer.generate_wordcloud_from_comments()
|
||||
except Exception as e:
|
||||
print(f"Error generating wordcloud: {e}")
|
||||
finally:
|
||||
# 确保爬虫结束后关闭浏览器
|
||||
if crawler:
|
||||
try:
|
||||
await crawler.close()
|
||||
print(f"[MediaCrawler] 浏览器已关闭")
|
||||
except Exception as e:
|
||||
print(f"[MediaCrawler] 关闭浏览器时出错: {e}")
|
||||
|
||||
|
||||
def cleanup():
|
||||
if crawler:
|
||||
# asyncio.run(crawler.close())
|
||||
pass
|
||||
if config.SAVE_DATA_OPTION in ["db", "sqlite"]:
|
||||
asyncio.run(db.close())
|
||||
# 注意:crawler.close() 已经在 main() 的 finally 块中调用
|
||||
# 这里只处理数据库关闭
|
||||
if config.SAVE_DATA_OPTION in ["db", "sqlite", "postgresql"]:
|
||||
try:
|
||||
asyncio.run(db.close())
|
||||
except Exception as e:
|
||||
print(f"[MediaCrawler] 关闭数据库连接时出错: {e}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user