Python 2.7 using twill, saving downloaded file properly

99封情书 提交于 2019-12-11 04:58:22

问题


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.



来源:https://stackoverflow.com/questions/12765486/python-2-7-using-twill-saving-downloaded-file-properly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!