网络爬虫定时爬取的相关方法

关于python的定时爬取相关方法:虽然time模块的time.sleep()方法使程序休眠来达到定时任务的目的,这样也可以,但是总觉得不是那么的专业,所以就使用如下python的定时任务模块APScheduler:

首先安装相关pip:pip install apscheduler

安装完成后即可以使用相关模块:

下面用一简单代码实现此模块的相关功能:

import time
from apscheduler.schedulers.blocking import BlockingScheduler

sched = BlockingScheduler()

@sched.scheduled_job('interval', seconds=5)
def my_job():
    print("时间如下:")
@sched.scheduled_job('interval', seconds=5)
def my_job():
    print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))

sched.start()

输出如下:
在这里插入图片描述
上述代码每隔5秒即自动执行此代码,不停的循环。
下面具体讲解此模块的相关功能:

1.添加相关的任务

from apscheduler.schedulers.blocking import BlockingScheduler

sched = BlockingScheduler()
@sched.scheduled_job('interval', seconds=5)
def my_job():
    print("定时")

sched.start()

2.移除相关的任务

job = scheduler.add_job(myfunc, 'interval', minutes=2)
job.remove()
#上述是移除一个任务
#下面是移除指定的任务,即添加id
sched.add_job(myfunc, 'interval', minutes=2, id='1')
sched.remove_job(“1”)

下面介绍
1.cron定时调度(某一时刻执行)
(int | str)表示参数既可以是int类型,也可以是str类型
(datetime | str)表示参数既可以是datetime类型,也可以是str类型

year(int | str) – 4 - digityear -(表示四位数的年份,如2008年)
month(int | str) – month(1 - 12) -(表示取值范围为1 - 12月)
day(int | str) – day of the(1 - 31) -(表示取值范围为1 - 31日)
week(int | str) – ISOweek(1 - 53) -(格里历2006年12月31日可以写成2006年 - W52 - 7(扩展形式或2006W527(紧凑形式))
day_of_week(int | str) – number or name of weekday(0 - 6 or mon, tue, wed, thu, fri, sat, sun) - (表一周中的第几天,既可以用0 - 6表示也可以用其英语缩写表示)
hour(int | str) – hour(0 - 23) - (表示取值范围为0 - 23时)
minute(int | str) – minute(0 - 59) - (表示取值范围为0 - 59分)
second(int | str) – second(0 - 59) - (表示取值范围为0 - 59秒)

# 表示2018年3月22日17时19分07秒执行该程序
sched.add_job(my_job, 'cron', year=2018, month=03, day=22, hour=17, minute=19, second=07)

# 表示任务在6,7,8,11,12月份的第三个星期五的00:00,01:00,02:00,03:00 执行该程序
sched.add_job(my_job, 'cron', month='6-8,11-12', day='3rd fri', hour='0-3')

# 表示从星期一到星期五5:30(AM)直到2014-05-30 00:00:00
sched.add_job(my_job(), 'cron', day_of_week='mon-fri', hour=5, minute=30, end_date='2014-05-30')

# 表示每5秒执行该程序一次,相当于interval 间隔调度中seconds = 5
sched.add_job(my_job, 'cron', second='*/5')

2.interval 间隔调度(每隔多久执行)
weeks (int) – number of weeks to wait
days (int) – number of days to wait
hours (int) – number of hours to wait
minutes (int) – number of minutes to wait
seconds (int) – number of seconds to wait
start_date (datetime|str) – starting point for the interval calculation
end_date (datetime|str) – latest possible date/time to trigger on
timezone (datetime.tzinfo|str) – time zone to use for the date/time calculations

#表示每隔3天17时19分07秒执行一次任务
sched.add_job(my_job, 'interval',days  = 03,hours = 17,minutes = 19,seconds = 07)

3.date 定时调度(作业只会执行一次)
run_date (datetime|str) – the date/time to run the job at -(任务开始的时间)
timezone (datetime.tzinfo|str) – time zone for run_date if it doesn’t have one already

# 在2009.11.6执行
sched.add_job(my_job, 'date', run_date=date(2009, 11, 6), args=['text'])
# 在2009.11.6日16:30:05执行
sched.add_job(my_job, 'date', run_date=datetime(2009, 11, 6, 16, 30, 5), args=['text'])

上述即为定时爬取的相关模块了!

扫描二维码关注公众号,回复: 4227543 查看本文章

猜你喜欢

转载自blog.csdn.net/qq_40594554/article/details/83039224