2014-12-12 64 views
0

我想保存站点中的所有图像。 wget是可怕的,至少对于http://www.leveldesigninspirationmachine.tumblr.com,因为在图像文件夹中它只是放弃HTML文件,并没有作为扩展。设置python脚本的输出位置

我发现了一个python脚本,使用是这样的:

[python] ImageDownloader.py URL MaxRecursionDepth DownloadLocationPath MinImageFileSize 

最后我得到了一些BeautifulSoup问题后运行该脚本。 但是,我无法在任何地方找到文件。我也试过“/”作为输出目录,希望这些图像能够成为我HD的根源,但没有运气。有人可以帮助我简化脚本,使其在终端中设置的cd目录中输出。或者给我一个应该工作的命令。我没有python的经验,我真的不想为一个2岁的脚本学习python,这可能甚至不会按我想要的方式工作。

另外,我怎样才能传递一个网站的数组?有了很多刮板,它给了我页面的前几个结果。 tumblr对滚动负载但没有任何效果,所以我想提前

# imageDownloader.py 
# Finds and downloads all images from any given URL recursively. 
# FB - 201009094 
import urllib2 
from os.path import basename 
import urlparse 
#from BeautifulSoup import BeautifulSoup # for HTML parsing 
import bs4 
from bs4 import BeautifulSoup 

global urlList 
urlList = [] 

# recursively download images starting from the root URL 
def downloadImages(url, level, minFileSize): # the root URL is level 0 
    # do not go to other websites 
    global website 
    netloc = urlparse.urlsplit(url).netloc.split('.') 
    if netloc[-2] + netloc[-1] != website: 
     return 

    global urlList 
    if url in urlList: # prevent using the same URL again 
     return 

    try: 
     urlContent = urllib2.urlopen(url).read() 
     urlList.append(url) 
     print url 
    except: 
     return 

    soup = BeautifulSoup(''.join(urlContent)) 
    # find and download all images 
    imgTags = soup.findAll('img') 
    for imgTag in imgTags: 
     imgUrl = imgTag['src'] 
     # download only the proper image files 
     if imgUrl.lower().endswith('.jpeg') or \ 
      imgUrl.lower().endswith('.jpg') or \ 
      imgUrl.lower().endswith('.gif') or \ 
      imgUrl.lower().endswith('.png') or \ 
      imgUrl.lower().endswith('.bmp'): 
      try: 
       imgData = urllib2.urlopen(imgUrl).read() 
       if len(imgData) >= minFileSize: 
        print " " + imgUrl 
        fileName = basename(urlsplit(imgUrl)[2]) 
        output = open(fileName,'wb') 
        output.write(imgData) 
        output.close() 
      except: 
       pass 
    print 
    print 

    # if there are links on the webpage then recursively repeat 
    if level > 0: 
     linkTags = soup.findAll('a') 
     if len(linkTags) > 0: 
      for linkTag in linkTags: 
       try: 
        linkUrl = linkTag['href'] 
        downloadImages(linkUrl, level - 1, minFileSize) 
       except: 
        pass 

# main 
rootUrl = 'http://www.leveldesigninspirationmachine.tumblr.com' 
netloc = urlparse.urlsplit(rootUrl).netloc.split('.') 
global website 
website = netloc[-2] + netloc[-1] 
downloadImages(rootUrl, 1, 50000) 
+2

程序应该将图像保存在同一目录下的程序跑。请注意,你不应该在你的程序中使用'except:pass',因为在下载过程中可能发生的任何错误只是被抑制,没有成功或失败的指示。特别是在尝试在程序中发现问题时。 – Frxstrem 2014-12-12 23:56:17

回答

1

由于Frxstream曾评论添加/page1

感谢,这个程序会在当前目录文件(即你在哪里运行它)。运行程序后,运行ls -l(或dir)查找它创建的文件。

如果它看起来还没有创建任何文件,那么很可能它确实没有创建任何文件,很可能是因为您的except: pass隐藏了一个异常。要查看发生了什么问题,请将try: ... except: pass替换为...,然后重新运行该程序。 (如果你不能理解和解决这个问题,请询问一个单独的StackOverflow问题。)

1

不看错误就很难分辨(+1关闭你的try/except块,所以你可以看到异常)但我看到一个错字这里:

fileName = basename(urlsplit(imgUrl)[2]) 

你没有“从进口里urlparse urlsplit”你有“进口里urlparse”所以你需要把它称为urlparse.urlsplit(),你必须在其他地方,所以应该是这样的

fileName = basename(urlparse.urlsplit(imgUrl)[2])