You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在GCP部署带SQLite与Twitter功能的Python定时脚本?

云服务器定时运行Python项目方案咨询

我有一个个人Python项目,目前每天手动运行脚本,希望在云服务器上实现定时自动化运行,达成“一劳永逸”的效果。

项目详情

该项目爬取多站点NBA数据,生成当日各赛事的预测结果,将结果发布至Twitter并保存至SQLite数据库,以便下次运行时校验。项目结构如下:

twitterBot
├── __pycache__
│	├── config.cpython-311.pyc
│	└── twitter.cpython-311.pyc
├── config.py
├── main.py
├── nba_models
│	├── __init__.py
│	├── __pycache__
│	│	├── __init__.cpython-311.pyc
│	│	├── config.cpython-311.pyc
│	│	└── get_picks.cpython-311.pyc
│	├── get_picks.py
│	├── nba_pick_data_2023.db
│	├── nba_pick_recent_data_2023.db
│	├── nba_scraper
│	│	├── __init__.py
│	│	├── __pycache__
│	│	│	├── __init__.cpython-311.pyc
│	│	│	└── scraper.cpython-311.pyc
│	│	└── scraper.py
│	└── vegas
│		├── __init__.py
│		├── __pycache__
│		│	├── __init__.cpython-311.pyc
│		│	└── vegas.cpython-311.pyc
│		└── vegas.py
├── test.ipynb
└── twitter.py

因原项目较复杂,我准备了包含核心功能的简化测试项目cloudTest,结构如下:

cloudTest
├── __pycache__
│	└── config.cpython-311.pyc
├── config.py
├── example.db
├── pic.png
└── test.py

test.py包含原项目核心功能:操作SQLite数据库、调用Twitter API发布内容等,代码如下:

import sqlite3
import os
import tweepy
import config

# Creates and insert some data in sqlite database
current_directory = os.getcwd()
db_file_path = os.path.join(current_directory, 'example.db')
conn = sqlite3.connect(db_file_path)
cur = conn.cursor()

table_creation_query = '''
CREATE TABLE IF NOT EXISTS example_table (
    id INTEGER PRIMARY KEY,
    name TEXT,
    age INTEGER
);
'''
cur.execute(table_creation_query)

# Get the maximum existing ID
max_id_query = 'SELECT MAX(id) FROM example_table'
cur.execute(max_id_query)
max_id = cur.fetchone()[0]

# Generate new data programmatically
new_name = "New User"
new_age = 25

# Increment the ID for the new row
new_id = max_id + 1 if max_id is not None else 1

# Insert new data into the table
insert_query = 'INSERT INTO example_table (id, name, age) VALUES (?, ?, ?)'
cur.execute(insert_query, (new_id, new_name, new_age))

# Commit the changes and close the connection

cur.execute(f"select * from example_table where id = {new_id}")
new_data = cur.fetchone()
conn.commit()
conn.close()


auth = tweepy.OAuth1UserHandler(config.CONSUMER_KEY, config.CONSUMER_SECRET)
auth.set_access_token(config.ACCESS_TOKEN, config.ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)

client = tweepy.Client(
            consumer_key=config.CONSUMER_KEY,
            consumer_secret=config.CONSUMER_SECRET,
            access_token=config.ACCESS_TOKEN,
            access_token_secret=config.ACCESS_TOKEN_SECRET,
        )


media = api.simple_upload(f"{current_directory}/pic.png")
client.create_tweet(text=f"Just inserted {new_data}", media_ids=[media.media_id])

我的疑问与需求

  • 刚注册GCP账号,有300美元额度,但对云服务不熟悉,不知道从何入手
  • 了解到Google Functions,但不确定项目规模是否适配,也不清楚SQLite在其中的运行方式,同时项目依赖多个非Python默认库,不知道如何处理
  • 看到有人推荐DigitalOcean,想了解是否适合我的需求
  • 希望得到方向指点,或相关学习资源

内容的提问来源于stack exchange,提问作者Dat Boi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 15:16:02