urllib downloading contents of an online directory

后端 未结 1 480
粉色の甜心
粉色の甜心 2021-01-05 08:43

I\'m trying to make a program that will open a directory, then use regular expressions to get the names of powerpoints and then create files locally and copy their content.

1条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-05 09:33

    This code worked for me. I just modified it a little because yours was duplicating each ppt file.

    from urllib2 import urlopen
    import re
    
    urlpath =urlopen('http://www.divms.uiowa.edu/~jni/courses/ProgrammignInCobol/presentation/')
    string = urlpath.read().decode('utf-8')
    
    pattern = re.compile('ch[0-9]*.ppt"') #the pattern actually creates duplicates in the list
    
    filelist = pattern.findall(string)
    print(filelist)
    
    for filename in filelist:
        filename=filename[:-1]
        remotefile = urlopen('http://www.divms.uiowa.edu/~jni/courses/ProgrammignInCobol/presentation/' + filename)
        localfile = open(filename,'wb')
        localfile.write(remotefile.read())
        localfile.close()
        remotefile.close()
    

    0 讨论(0)
提交回复
热议问题