ディレクトリを開くプログラムを作ってから、正規表現を使ってパワーポイントの名前を取得し、ファイルをローカルに作成してその内容をコピーしようとしています。私はこれを実行すると動作するように見えますが、実際にファイルを開こうとするとバージョンが間違っていると言い続けます。Python urllibオンラインディレクトリの内容をダウンロードする
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()
** RegExでHTMLを解析しないでください。http://stackoverflow.com/a/1732454/851737を参照してください。 lxmlやBeautifulSoupのようなHTML解析ライブラリを使用してください。 – schlamar
美しいです。あなたのお勧めをありがとう。 – davelupt