ウェブページからファイルをダウンロードしたい。そのWebページには1つの.zipファイルしかありません(ダウンロードしたいものですが).zipファイルをクリックするとダウンロードが開始されますが、URLは変更されません(URLはまだhttp://ldn2800:8080/id=2800のままです)。 http://example.com/1.zip
という形式のURLがないと考えると、どうすればこのファイルをPythonでダウンロードできますか?URLが変更されないときにPythonを使用してファイルをダウンロードする方法
また、直接http://ldn2800:8080/id=2800に行くと、そのページは.zipファイルで開きますが、クリックしないとダウンロードされません。どうすればPythonを使ってダウンロードできますか?
UPDATE:今、私はそれをこのようにやっている:
if (str(dict.get('id')) == winID):
#or str(dict.get('id')) == linuxID):
#if str(dict.get('number')) == buildNo:
buildTypeId = dict.get('id')
ID = dict.get('id')
downloadURL = "http://example:8080/viewType.html?buildId=26009&tab=artifacts&buildTypeId=" + ID
directory = BindingsDest + "\\" + buildNo
if not os.path.exists(directory):
os.makedirs(directory)
fileName = None
if buildTypeId == linuxID:
fileName = linuxLib + "-" + buildNo + ".zip"
elif buildTypeId == winID:
fileName = winLib + "-" + buildNo + ".zip"
if fileName is not None:
print(dict)
downloadFile(downloadURL, directory, fileName)
def downloadFile(downloadURL, directory, fileName, user=user, password=password):
if user is not None and password is not None:
request = requests.get(downloadURL, stream=True, auth=(user, password))
else:
request = requests.get(downloadURL, stream=True)
with open(directory + "\\" + fileName, 'wb') as handle:
for block in request.iter_content(1024):
if not block:
break
handle.write(block)
しかし、それだけで、必要な場所にzipファイルを作成しますが、そのジッパーが開かれ、何もしていないことはできません。 このようなことができますか?ウェブページ上のファイル名を検索して、そのパターンをダウンロードしたのですか?
これはあなたを助けるはずです:http://stackoverflow.com/questions/11002014/downloading-file-with-python-mechanize – bernie
pythonを使ってリクエストを実行しようとしましたか?何が起こるのですか? –
@MarcoAcierno私は今何をしているのかを示す質問を更新しました。 – Arshad