python - urllib 下载在线目录的内容

标签 python python-3.x directory urllib2 urllib

我正在尝试制作一个程序,它将打开一个目录,然后使用正则表达式获取 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()

最佳答案

这段代码对我有用。我只是对其进行了一些修改,因为您正在复制每个 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()

关于python - urllib 下载在线目录的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10875215/

相关文章:

Python subprocess.CalledProcessError : Command 'adb devices' returned non-zero exit status 127

python - 按startwith match排序查询结果

python - PyAutoGUI 图像识别 - 像素颜色匹配的小问题

python - 如何在 Python 中输入提示某种数据类型的(任意)集合?

python - 如何在连续的 matplotlib 图形之间添加空格?

php - Laravel 文件路径

linux - 令人困惑的 Makefile

azure - 如何设置 Azure Active Directory 以支持单点登录

python - 用于计算 DNA 序列中 GC 含量的初级 Python 脚本

Python 的正则表达式模块 : repeating 'backreferences' does not appear to work correctly