Python 2.7 using twill, saving downloaded file pro

2019-07-22 19:48发布

问题:

import twill
twill.commands.agent("Mozilla/5.0 (Windows NT 6.2; WOW64; rv:15.0) Gecko/20120910144328 Firefox/15.0.2")
twill.commands.go('loginpage...')

twill.commands.fv("1", "txtUserName", "username")
twill.commands.fv("1", "txtPassword", "password")

# Login Page
twill.commands.submit()

# Pre Registration Report Selection
twill.commands.submit()

# Generate and download the report.
twill.commands.go("ReportGenerator.ashx")

print "Done!"

twill.commands.redirect_output('PreRegistration.xls')
#twill.commands.save_html('PreRegistration.xls')

That is what i'm attempting to use currently.

The website works as such:

  1. Login page redirects to another form that lets me choose the report to generate.
  2. I want all default choices so i just submit the page again.
  3. I then visit another page which generates the report and then force downloads the file.

redirect_output does not save the xls file.

save_html does save the information BUT it corrupts something and Excel is unable to read it at all. I use kdiff to checkout any differences, it claims there are lots but the data on left/right is identical, wondering if it's an issue with line-endings..etc. Not 100% sure.

I have checked the commands page on twill's website, but i'm unsure how to go about this.

回答1:

As i suspected it was a line-ending issue. Modified twill's save_html function to write as 'wb' instead of 'w', all is well.



标签: python twill