Python 2.7使用斜纹,正确保存下载的文件 [英] Python 2.7 using twill, saving downloaded file properly

查看:87
本文介绍了Python 2.7使用斜纹,正确保存下载的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

网站的工作方式如下:

  1. 登录"页面重定向到另一个表单,该表单使我可以选择要生成的报告.
  2. 我想要所有默认选项,所以我只需要再次提交页面即可.
  3. 然后我访问另一个页面,该页面生成报告,然后强制下载文件.

redirect_output不保存xls文件.

redirect_output does not save the xls file.

save_html确实保存了信息,但它破坏了某些内容,而Excel根本无法读取它.我使用kdiff来检查任何差异,它声称有很多区别,但是左/右的数据是相同的,想知道这是否是行尾的问题..etc.不确定100%.

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.

我已经检查了twill网站上的命令页面,但是我不确定该怎么做.

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

推荐答案

我怀疑这是行尾问题.修改后的斜纹纸的save_html函数写为"wb"而不是"w",一切都很好.

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 2.7使用斜纹,正确保存下载的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆