2016-12-12 15 views
-1

私は認証を管理し、Webサイトから画像をダウンロードするには多くのソリューションを見てきましたが、私はこれらのライブラリのすべての中で失わ少しだ:ウェブサイトから画像を認証してダウンロードする方法は?

  • urllibは
  • urllib2の
  • pycurl
  • リクエスト
  • 私は理解していなかった他の暗いソリューション...

は基本的に、私が取得したいです認証が必要なウェブサイトの画像。 Python-2.7でこれを行う最も簡単な方法は何ですか?

ありがとうございます。

答えて

0

docのリクエストを見ることができます。

requests.get('http://example.com/image.png', auth=HTTPBasicAuth('user', 'pass')) 
0

私は最終的にのみrequestsでそれを行うために管理:たとえば、あなたは基本的なHTTP認証が必要な場合。

import requests 

url_login = '' 
url_image = '' 

username = '' 
password = '' 

# Start a session so we can have persistant cookies 
session = requests.session() 

# This is the form data that the page sends when logging in 
login_data = { 
    'login': username, 
    'password': password, 
    'submit': 'Login' 
} 

# Authenticate 
r = session.post(url_login, data=login_data) 

# Download image 
with open('output.png', 'wb') as handle: 
    response = session.get(url_image, stream=True) 

    if not response.ok: 
     print "Something went wrong" 
     return False 

    for block in response.iter_content(1024): 
     handle.write(block) 

    handle.close() 
関連する問題