python怎么重复执行代码_Twitter Scraping重复执行代码(python)

这是一个Twitter抓取代码,用于提取包含着名关键字的推文.

我想每12小时重复下面的整个代码. (或12小时10分钟休息).你可以给我重复短语的建议吗?

import tweepy

import time

import os

import json

import simplejson

search_term = 'word1'

search_term2= 'word2'

search_term3='word3'

lat = "xxxx"

lon = "xxxx"

radius = "xxxx"

location = "%s,%s,%s" % (lat, lon, radius)

API_key = "xxxx"

API_secret = "xxxx"

Access_token = "xxxx"

Access_token_secret = "xxxx"

auth = tweepy.OAuthHandler(API_key, API_secret)

auth.set_access_token(Access_token, Access_token_secret)

api = tweepy.API(auth)

c=tweepy.Cursor(api.search,

q="{}+OR+{}".format(search_term, search_term2, search_term3),

rpp=1000,

geocode=location,

include_entities=True)

data = {}

i = 1

for tweet in c.items():

data['text'] = tweet.text

print(i, ":", data)

i += 1

time.sleep(1)

wfile = open(os.getcwd()+"/workk2.txt", mode='w')

data = {}

i = 0

for tweet in c.items():

data['text'] = tweet.text

wfile.write(data['text']+'\n')

i += 1

wfile.close()

最佳答案 您可以设置一个每12小时执行一次脚本的Cron作业.为此,您应该使用.py扩展名保存脚本并使其可执行.然后将其添加到您的crontab:

0 0 0/12 * * ? /usr/bin/python yourscript.py

有关详细信息,请查看this问题.或者,python中有一些软件包(例如APScheduler)可以帮助您实现这一目标.在APScheduler中,您可以定义这样的作业:

from apscheduler.schedulers.blocking import BlockingScheduler

sched = BlockingScheduler()

@sched.scheduled_job('interval', hours=12)

def timed_job():

print('This job is run every 12 hours.')

sched.configure(options_from_ini_file)

sched.start()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值