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'
4

1 回答 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()
于 2012-12-08T13:30:41.580 回答