LoginSignup
4
6

More than 5 years have passed since last update.

Python手遊び(wget)

Last updated at Posted at 2019-01-27

この記事、何?

pythonを使って、Windowsでhttpsからファイルを取得するという話。

ちょっと前の記事でWindowsでwget代わりにPowerShellでInvoke-WebRequestしてた。
ただ、https相手だとうまくいかなかった。
それをトラブルシュートするよりpythonしたほうが楽っぽかったので頼ってみた、という話。

どういう人向け?

まあ・・・自分あて。
クローニングとかもいろいろ潤沢な時代に「httpsあてでもファイル取れるわ~」って言われても、ね?
でもQiitaの記事を取れるバッチファイルも付けたのでよければどうぞ。

参考にした記事

結局は他力本願なんですけどね。
Alice1017さん、ありがとうございます。

■Pythonでバイナリファイルを保存する
https://qiita.com/Alice1017/items/34befe8168cd771f535f

やったこと

Pythonスクリプト1つ、呼出用のバッチが一つ。


#17-01.py
#Qiitaの投稿ID(?)をもらって、Markdownをファイル化
import sys
import shutil
import requests
args = sys.argv
id = args[1]
URL = 'https://qiita.com/siinai/items/' + id + '.md'
print(URL)
filepath = id + '.md'
print(filepath)
res = requests.get(URL, stream = True)
with open(filepath, 'wb') as fp:
    shutil.copyfileobj(res.raw, fp)

rem 呼び出し用のバッチファイル
rem python 17-01.py [qiita-id]
python 17-01.py 4deb8529a2224a238b91
python 17-01.py 7b4196f4271448093a1f
python 17-01.py 457977b61b6c4cb15ac4
...

感想

まあ・・・普通。
Pythonってこんなに簡単なのね・・・
いいわぁ♪

4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6