1. 程式人生 > >網路爬蟲定時爬取的相關方法

網路爬蟲定時爬取的相關方法

關於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'])

上述即為定時爬取的相關模組了!