Python:执行请求时将变量传递给url

2024-10-04 11:29:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我用下面的方法调用一个url,在调用它的时候需要传递year/month/day作为变量 最后的url结构是

http://mywebiste.com/downloading/main/2016/03/28/'

这是我的密码

import pycurl
import os, sys, re, shutil
import datetime
import time
import requests
import datetime
import logging
import httplib

#logging.basicConfig(level=logging.DEBUG)

httplib.HTTPConnection.debuglevel = 1
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True

now = datetime.datetime.now()
year = now.year
month = now.month
day =  now.day

url = 'http://mywebiste.com/downloading/main/%s/%s/%s/'
r = requests.get(url,timeout=10)
r.text
r.status_code
r.connection.close()

Tags: debugimportcomloghttpurldatetimelogging
1条回答
网友
1楼 · 发布于 2024-10-04 11:29:43

我想你想要这个:

url = 'http://mywebiste.com/downloading/main/%s/%s/%s/' % (year, month, day)

再解释一下:对于每个要与%一起使用的字符串,您需要将字符串用作my_string = 'my value is: %s' % value,其中value是您的变量。在

相关问题 更多 >