Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
请求中的Python 3变量导致404?_Python_Http Status Code 404_Urllib_Python 3.4 - Fatal编程技术网

请求中的Python 3变量导致404?

请求中的Python 3变量导致404?,python,http-status-code-404,urllib,python-3.4,Python,Http Status Code 404,Urllib,Python 3.4,我有两个列表,一个是日期列表,另一个是小时列表,我正在浏览一个网站寻找内容 date = ['1','2','3'] hour = ['1','2','3'] 我编写了以下while/for来循环查看日期和时间,并打开所有组合: datetotry = 0 while (datetotry < len(date)): for i in range(len(hour)): print ('https://www.website.org

我有两个列表,一个是日期列表,另一个是小时列表,我正在浏览一个网站寻找内容

date = ['1','2','3']
hour = ['1','2','3']
我编写了以下
while
/
for
来循环查看日期和时间,并打开所有组合:

datetotry = 0  
while (datetotry < len(date)):
    for i in range(len(hour)):              
        print ('https://www.website.org/backups/backup_2004-09-'+date[datetotry]+'_'+hour[i]+".sql")
        html = opener.open('https://www.website.org/backups/backup_2004-09-'+date[datetotry]+'_'+hour[i]+'.sql').read()
datetotry += 1
节,而不是列表中的项目,如打印到控制台时

这是否意味着我必须执行类似于
urllib.parse.urlencode
的操作来实际替换变量

我导入了文章中提到的库,并将代码更改为:

 from urllib.error import URLError, HTTPError
 from urllib.request import Request, urlopen

while (datetotry < len(date)):
for I in range(len(hour)):              
    HTML = Request('https://www.website.org/backups/backup_2004-09-'+date[datetotry]+'_'+hour[i]+'.sql')
    try:
        response = urlopen(html)
    except URLError as e:
        print('The server couldn\'t fulfill the request.')
        print('Error code: ', e.code)
    except URLError as e:
        print('We failed to reach a server.')
        print('Reason: ', e.reason)
    else:
从urllib.error导入urleror,HTTPError
从urllib.request导入请求,urlopen
while(datetotry

因此代码实际运行,而不是因为返回404而停止。查看其实际请求的最佳方式是什么?我是否必须进行某种编码?编程新手,尤其是Python3

白天和小时不应该是零填充的吗?你能给我们一个有效的工作URL吗?如果你不局限于stdlib,还有请求库
 from urllib.error import URLError, HTTPError
 from urllib.request import Request, urlopen

while (datetotry < len(date)):
for I in range(len(hour)):              
    HTML = Request('https://www.website.org/backups/backup_2004-09-'+date[datetotry]+'_'+hour[i]+'.sql')
    try:
        response = urlopen(html)
    except URLError as e:
        print('The server couldn\'t fulfill the request.')
        print('Error code: ', e.code)
    except URLError as e:
        print('We failed to reach a server.')
        print('Reason: ', e.reason)
    else: