使用urllib下载在线目录的内容

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

2
你绝不能使用正则表达式解析HTML,参见https://dev59.com/X3I-5IYBdhLWcg3wq6do#1732454。应该使用像lxml或BeautifulSoup这样的HTML解析库。 - schlamar
BeautifulSoup就是它了。感谢您的推荐。 - davelupt
1个回答

10

这段代码对我有用。我只是稍微修改了一下,因为你的代码会复制每个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()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接