Python: 'curl'不被识别为内部或外部命令、可操作的程序或批处理文件

3
import commands
import os
import pickle

def readDir():
    directory = raw_input('In which folder would you like to save the files?? \n')
    if(os.path.exists(directory)):
            print 'Error!! Please give an other name '
            directory = raw_input('In which folder would you like to save the file??\n')
            os.mkdir(directory)
            os.chdir(directory)
    else:
        os.mkdir(directory)
        os.chdir(directory)


readDir()
url = raw_input('Which url are you aiming at ?\n')
tmp = open('tempo.txt','w');
tmp.writelines(url)
tmp.close()
tmp = open('tempo.txt','r');
link = tmp.read()
os.system(" curl " + link +"| egrep -o 'http:.*All\.ram'  > final.txt  ")



infile = open('final.txt', 'r')
outfile = open('tmp.txt', 'w')



for line in infile:

outfile = open('tmp.txt', 'w')
key = line
list = key.split("/")
dir = list[6]
outfile.writelines(key)
outfile.close()
open('tmp.txt','r')
os.system("cat tmp.txt | xargs -n1 -i curl {} > links")
os.system("wget -P %s -i links" %dir)

infile.close()
outfile.close()
os.remove(outfile.name)
os.remove('links')
os.remove(tmp.name)

错误:我只是以谷歌为例。

Which url are you aiming at ?
google.com
'curl' is not recognized as an internal or external command, operable program or batch file.
Traceback (most recent call last):
File "C:\Users\User\Desktop\download.py", line 52, in <module>
infile = open('final.txt', 'r')
IOError: [Errno 2] No such file or directory: 'final.txt'
1个回答

1

我看到的问题:

  1. 在您的本地计算机上似乎没有安装curl。不确定为什么您想要为仅抓取URL而进行系统调用...
  2. 由于您没有安装curl,因此从未创建final.txt。当您尝试在系统中稍后加载它时,它无法工作。

底线是,找到一种不使用curl的方法,您会更好。

os.system(" curl " + link +"| egrep -o 'http:.*All\.ram'  > final.txt  ")

仔细看来,您似乎只想下载文件。使用urllib直接下载要容易得多。我将从python文档中复制一个简单的示例,让您自行了解如何使用它。请注意,如果您使用的是Python 2或3,则在执行此操作时有很大的区别,所以请注意...
>>> import urllib
>>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
>>> f = urllib.urlopen("http://www.musi-cal.com/cgi-bin/query?%s" % params)
>>> print f.read()

谢谢,有其他方式可以获取URL而不使用curl吗? - Terrii
我不太确定你想做什么,但通常来说,调用系统命令是不好的Python编程实践...具体来说,我很难理解的是输出部分... - PearsonArtPhoto
是这样的,我想要做的是将在线目录中的所有SWF文件下载到我的电脑上。这有帮助吗? - Terrii
我会直接使用 URLLib 读取文件并将其写入。这样应该能正常工作。 - PearsonArtPhoto
抱歉,你能给个例子吗?我刚开始学Python,还没有深入研究过URLLib。 - Terrii

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接