我是python的新手
使用Python 2.7我想要实现的是我在脚本中获得了url列表,每个url需要在cronjob中的特定日期运行。我需要import datetime
和import time
获取服务器日("%A")
并与当天运行的网址匹配,这样可以为每个执行相同脚本的网址节省我的费用
import urllib2,urllib
import re,os
import datetime
import time
mylist={
monday:'http://www.trt.tv/baba-candir/bolumler/80212',
thusday:'http://www.trt.tv/filinta/bolumler/20198',
wensday:'http://www.trt.tv/dirilis-ertugrul/bolumler/20196',
}
for days in mylist:
print days
req = urllib2.Request(url)
req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; az-AZ; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
response = urllib2.urlopen(req)
link=response.read()
response.close()
答案 0 :(得分:1)
from datetime import date
today = date.today().strftime('%A')
例如,如果今天是星期一,那么这将在周一返回'如果你想要它是小写的,你可以这样做:today.lower()
所有在一起:
import urllib2,urllib
import re,os
import time
from datetime import date
mylist = {
'Monday':'http://www.trt.tv/baba-candir/bolumler/80212',
'Thursday':'http://www.trt.tv/filinta/bolumler/20198',
'Wednesday':'http://www.trt.tv/dirilis-ertugrul/bolumler/20196',
}
today = date.today().strftime('%A')
if today in mylist:
url = mylist[today]
req = urllib2.Request(url)
req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; az-AZ; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
response = urllib2.urlopen(req)
link=response.read()
response.close()
答案 1 :(得分:0)
正如this answer所述,可以从
获取日期import datetime
print(datetime.datetime.today().weekday())
# prints '0' because it's Monday
打印0(星期一)到6(星期日)之间的数字。所以,在你的代码中(如果我理解正确的话):
import urllib2,urllib
import re,os
import datetime
import time
mylist={
0:'http://www.trt.tv/baba-candir/bolumler/80212',
1:'http://www.trt.tv/filinta/bolumler/20198',
2:'http://www.trt.tv/dirilis-ertugrul/bolumler/20196',
}
today = datetime.datetime.today().weekday()
if today in mylist:
# Forgotten?
url = mylist[today]
# Forgotten?
req = urllib2.Request(url)
req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; az-AZ; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
response = urllib2.urlopen(req)
link=response.read()
response.close()
else:
print('error: today (day %s) was not found in mylist.' % today)