Tensorflow官方文档MNIST初级教程中,input_data不能正常爬取数据问题

使用Tensorflow官方文档中提供的MNIST教程中的input_data.py文件,运行后返回HTTP Error 403: Forbidden问题。
在网上查到是因为网站反爬虫的问题,然后根据一篇博客的方法修改input_data.py文件的代码,顺利解决问题。
https://www.cnblogs.com/fenglj/p/7291343.html
**解决的方法:**修改input_data中maybe_downlaod()方法的代码:

原先的代码

def maybe_download(filename, work_directory):
    """Download the data from Yann's website, unless it's already here."""
    if not os.path.exists(work_directory):
        os.mkdir(work_directory)
    filepath = os.path.join(work_directory, filename)
    if not os.path.exists(filepath):
        filepath, _ = urllib.request.urlretrieve(SOURCE_URL + filename, filepath)
        statinfo = os.stat(filepath)
        print('Successfully downloaded', filename, statinfo.st_size, 'bytes.')

修改后的代码:(加多了三句)

def maybe_download(filename, work_directory):
    """Download the data from Yann's website, unless it's already here."""
    if not os.path.exists(work_directory):
        os.mkdir(work_directory)
    filepath = os.path.join(work_directory, filename)
    if not os.path.exists(filepath):
        opener=urllib.request.build_opener()
        opener.addheaders=[('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1941.0 Safari/537.36')]
        urllib.request.install_opener(opener)
        filepath, _ = urllib.request.urlretrieve(SOURCE_URL + filename, filepath)
        statinfo = os.stat(filepath)
        print('Successfully downloaded', filename, statinfo.st_size, 'bytes.')

你可能感兴趣的:(深度学习框架,tensorflow)