Python Twill: Download File Accessible Through Php Script
I use twill to navigate on a website protected by a login form. from twill.commands import * go('http://www.example.com/login/index.php') fv('login_form', 'identifiant', 'login')
Solution 1:
I managed to do it sending the cookie jar from twill
to requests
.
Nota: I could not use requests
only due to an intricate control at login (was not able to figure out the correct headers or other options).
import requests
from twill.commands import *
# showing login form with twill
go('http://www.example.com/login/index.php')
showforms()
# posting login form with twill
fv("login_form", "identifiant", "login")
fv("login_form", "password", "pass")
formaction("login_form", "http://www.example.com/login/control.php")
submit()
# getting binary content with requests using twill cookie jar
cookies = requests.utils.dict_from_cookiejar(get_browser()._session.cookies)
url = 'http://www.example.com/util/exports/control.php?action=export'withopen('out.xls', 'wb') as handle:
response = requests.get(url, stream=True, cookies=cookies)
ifnot response.ok:
raise Exception('Could not get file from ' + url)
for block in response.iter_content(1024):
handle.write(block)
Solution 2:
Another way using twill.commands.save_html
modified to write as 'wb' instead of 'w': Python 2.7 using twill, saving downloaded file properly
Post a Comment for "Python Twill: Download File Accessible Through Php Script"