Python从URL下载错误?

时间:2012-12-08 13:25:45

标签: python downloading

任何人都可以看到这个代码的问题,因为它不断回复错误,这是代码,并在代码下发布错误...感谢高级帮助......

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 个答案:

答案 0 :(得分:1)

我看到的问题:

  1. curl似乎未在您的本地计算机上安装。不知道你为什么要进行系统调用只是为了抓取一个URL ...
  2. 由于您没有安装curl,因此没有创建final.txt。当您稍后尝试在系统中加载它时,它不起作用。
  3. 最重要的是,找到一种不使用卷曲的方法,你会更好。

    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()