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

时间:2016-03-28 11:50:51

标签: python python-2.7

我正在使用以下内容来调用网址,我需要在调用它时将年/月/日作为变量传递 所以最终的网址结构就像

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()

1 个答案:

答案 0 :(得分:4)

我想你想要这个:

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

更多解释:对于每个字符串,您希望使用%方式的变量,您需要将字符串用作my_string = 'my value is: %s' % value,其中value是您的变量。