SORU
15 HAZİRAN 2010, Salı


Urllib ile bir resim indirme ve python

Webcomics indir ve benim masaüstünde bir klasör içine yerleştirir bir Python komut dosyası yapmak için çalışıyorum. Benzer bir şey burada birkaç benzer programlar buldum ama hiçbir şey istediğim gibi. En benzer bulduğum kişi burada (http://bytes.com/topic/python/answers/850927-problem-using-urllib-download-images). Bu kodu kullanarak çalıştı:

>>> import urllib
>>> image = urllib.URLopener()
>>> image.retrieve("http://www.gunnerkrigg.com//comics/00000001.jpg","00000001.jpg")
('00000001.jpg', <httplib.HTTPMessage instance at 0x1457a80>)

Ben o zaman bir dosya için bilgisayarımı aradı "00000001.jpg" ama hepsi bu önbelleğe alınan resim buldum. Hatta benim bilgisayara dosya kurtardı emin değilim. Dosyasını karşıdan nasıl anlıyorum sonra, gerisi nasıl idare edeceğimi biliyorum. Aslında sadece bir for döngüsü kullanın ve dize de split '00000000'.'jpg' ve artış '00000000 bir şekilde belirlemek gerek.' en büyük sayı kadar, Doğru dosyayı karşıdan yükleme yapmak için en iyi yolu herhangi bir reccomendations?

Teşekkürler!

6/15/10 DÜZENLEYİN

İşte tamamlanan komut, seçtiğiniz herhangi bir dizine dosyaları kaydeder. Bazı garip nedenle, dosya indirme değildi ve oldu da. O kadar temiz ve üzerinde herhangi bir öneriniz çok takdir olurdu. İstisnalar belirli sayıda yetiştirilir sonra şu anda pek çok çizgi roman sadece son bir program çıkmak yerine getirebilirim çok sitede var dışarı bulmak için nasıl bir çalışma yaparım.

import urllib
import os

comicCounter=len(os.listdir('/file')) 1  # reads the number of files in the folder to start downloading at the next comic
errorCount=0

def download_comic(url,comicName):
    """
    download a comic in the form of

    url = http://www.example.com
    comicName = '00000000.jpg'
    """
    image=urllib.URLopener()
    image.retrieve(url,comicName)  # download comicName at URL

while comicCounter <= 1000:  # not the most elegant solution
    os.chdir('/file')  # set where files download to
        try:
        if comicCounter < 10:  # needed to break into 10^n segments because comic names are a set of zeros followed by a number
            comicNumber=str('0000000' str(comicCounter))  # string containing the eight digit comic number
            comicName=str(comicNumber ".jpg")  # string containing the file name
            url=str("http://www.gunnerkrigg.com//comics/" comicName)  # creates the URL for the comic
            comicCounter =1  # increments the comic counter to go to the next comic, must be before the download in case the download raises an exception
            download_comic(url,comicName)  # uses the function defined above to download the comic
            print url
        if 10 <= comicCounter < 100:
            comicNumber=str('000000' str(comicCounter))
            comicName=str(comicNumber ".jpg")
            url=str("http://www.gunnerkrigg.com//comics/" comicName)
            comicCounter =1
            download_comic(url,comicName)
            print url
        if 100 <= comicCounter < 1000:
            comicNumber=str('00000' str(comicCounter))
            comicName=str(comicNumber ".jpg")
            url=str("http://www.gunnerkrigg.com//comics/" comicName)
            comicCounter =1
            download_comic(url,comicName)
            print url
        else:  # quit the program if any number outside this range shows up
            quit
    except IOError:  # urllib raises an IOError for a 404 error, when the comic doesn't exist
        errorCount =1  # add one to the error count
        if errorCount>3:  # if more than three errors occur during downloading, quit the program
            break
        else:
            print str("comic"  ' '   str(comicCounter)   ' '   "does not exist")  # otherwise say that the certain comic number doesn't exist
print "all comics are up to date"  # prints if all comics are downloaded

CEVAP
15 HAZİRAN 2010, Salı


urllib.urlretrieve kullanarak:

import urllib
urllib.urlretrieve("http://www.gunnerkrigg.com//comics/00000001.jpg", "00000001.jpg")

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • GOTO Conferences

    GOTO Confere

    3 EKİM 2011
  • MrMimoB

    MrMimoB

    11 NİSAN 2011
  • picster

    picster

    20 NİSAN 2006