Heim > Fragen und Antworten > Hauptteil
大神好,我在python中,想在
http://paper.people.com.cn/rm...
后面加上一年的年月日,
就是变成:
http://paper.people.com.cn/rm...
http://paper.people.com.cn/rm...
http://paper.people.com.cn/rm...
http://paper.people.com.cn/rm...
....
http://paper.people.com.cn/rm...
http://paper.people.com.cn/rm...
该用什么办法呢?
刚看了自己的提问,没显示全,补充上图片,谢谢。
PHP中文网2017-04-18 10:26:39
datetime模块可以生成时间序列,然后字符串拼接。
如果是爬虫,Requests模块里requests.get()里参数也可以拼接。
额。。。允许我说一种比较笨的方法。
我主要做数据分析及可视化的,而且不是计算机出身,遇到问题我首先都是找现成的函数。
import datetime
from datetime import datetime as dt
startday = dt(2016, 1, 1)
ds = []
span = datetime.timedelta(1)
for i in range(365 * 2 + 1):
ds.append(startday)
startday += span
dss = [i.strftime('%Y-%m/%d') for i in ds]
rawurl = 'http://paper.people.com.cn/rmrb/html/'
url = [rawurl + i for i in dss]
阿神2017-04-18 10:26:39
python取过去具体时间的方法:
#!/usr/bin/python
import time
#取一天前的当前具体时间
time.strftime('%Y-%m-%d %T',time.localtime(time.time()-24*60*60))
#取15天前的当前具体时间
time.strftime('%Y-%m-%d %T',time.localtime(time.time()-15*24*60*60))
#取15天前当前具体时间的前2小时
time.strftime('%Y-%m-%d %T',time.localtime(time.time()-15*24*60*60-2*60*60))
python取将来具体时间的方法:
#!/usr/bin/python
import time
#取一天后的当前具体时间
time.strftime('%Y-%m-%d %T',time.localtime(time.time()+24*60*60))
#取20天后的当前具体时间
time.strftime('%Y-%m-%d %T',time.localtime(time.time()+20*24*60*60))
#取20天后当前具体时间的前2小时
time.strftime('%Y-%m-%d %T',time.localtime(time.time()+20*24*60*60-2*60*60))
具体可以看看这个Python 时间操作例子和时间格式化参数小结 http://www.php.cn/python-tuto...