我已经看到很多解决方案来管理身份验证并从网站下载图像,但我在所有这些库中有点丢失:
基本上,我想从需要身份验证的网站检索图像。在python-2.7中最简单的方法是什么?
谢谢。
答案 0 :(得分:0)
您可以查看请求doc。例如,如果您需要基本的HTTP身份验证:
requests.get('http://example.com/image.png', auth=HTTPBasicAuth('user', 'pass'))
答案 1 :(得分:0)
我终于设法只使用requests
。
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()