2012-12-08 83 views
3

任何人都可以看到一个问题,此代码,因为它正在添加保持背部有一个错误,这是代码和生病后的代码下的错误...感谢先进的帮助...Python从URL错误下载?

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. 卷曲似乎并没有被你的本地计算机上安装。不知道为什么你想要一个系统调用,只抓取一个URL ......
  2. 因为你没有卷曲安装,所以没有创建任何final.txt。当您尝试在系统中稍后加载它时,它不起作用。

底线,找到一种方法来做到这一点,而不使用卷曲,你会更好。

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

更仔细地看,它看起来像你只是想下载文件。直接用urllib做这件事更容易。我将复制python docs中的一个简单示例,并让您从中找出如何使用它。注意,还有如何,如果你正在使用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() 
+0

感谢在那里,我可以抓住的网址,而不是使用curl任何其他方式? – Terrii

+0

是的,使用urllib。 http://docs.python.org/2/library/urllib.html – PearsonArtPhoto

+0

谢谢,但我不能解决它:/ – Terrii