【问题标题】:Python: how to save the output to a text file?Python:如何将输出保存到文本文件?
【发布时间】:2016-07-30 05:04:15
【问题描述】:

1。 我试过 -o 哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇 2.and >>text.txtrr 呸呸呸呸呸呸

    # -*- coding: iso-8859-15
    import sys
    import requests
    import os
    if len(sys.argv) >= 2:
            os.system('clear') 
            print "Please wait.."`enter code here`
            P1 = str(sys.argv[1])
            url = "http://example.com/index.php"
            data= {
               'getp1':'%s'%(P1)
            }
            r = requests.get(url,params=data) -o text.txt
            os.system('clear')
            print r.content 

    else:
            print "[ERROR] Este programa nerrcesita un parámetro";

【问题讨论】:

  • 你有什么问题?

标签: python save output


【解决方案1】:

首先,这不是一个很好的问题。你没有说明你的错误是什么,或者你期望发生什么。但是,我们可以修复您的代码中的一些明显问题。

在您使用 sys.argv 之后,您似乎混淆了 shell 命令和 python 代码。

r = requests.get(url,params=data) -o text.txt

不是有效的 python 代码 - 看起来您正在尝试在那里使用命令行语法。

我建议如下:

r = requests.get(url,params=data) 
f = open('text.txt', 'w' )
f.write(r.content)
f.close()

还值得注意的是,鉴于您正在下载 PHP 文件内容,您可能需要 r.text 函数,而不是 r.content,which is for non-text responses (like images). you should also set the encoding.

【讨论】:

    猜你喜欢
    • 2014-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多