urllib下载在线目录的内容

发布于 2021-01-29 16:23:28

我正在尝试制作一个程序,该程序将打开目录,然后使用正则表达式获取PowerPoint的名称,然后在本地创建文件并复制其内容。当我运行它时,它似乎可以工作,但是当我实际尝试打开文件时,他们一直说版本是错误的。

from urllib.request 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:
    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
被浏览
41
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    这段代码对我有用。我只是对其进行了一点修改,因为您正在复制每个ppt文件。

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


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看