Ho cercato su Google e sui suoi archivi. Ci sono molti buoni articoli, ma nessuno sembra aiutarmi. Quindi ho pensato di venire qui per una risposta più specifica.Python - Come si esegue un file .py?
L'obiettivo: voglio eseguire this code su un sito Web per ottenere tutti i file di immagini contemporaneamente. Risparmierà un sacco di puntamenti e clic.
Ho Python 2.3.5 su un computer Windows 7 x64. È installato in C: \ Python23.
Come ottengo questo script per "andare", per così dire?
=====================================
WOW. 35k visualizzazioni. Visto che questo è risultato superiore su Google, ecco un link utile che ho trovato nel corso degli anni:
http://learnpythonthehardway.org/book/ex1.html
Per l'installazione, consultare esercizio 0.
========= =============
FYI: Ho zero esperienze con Python. Tutto il consiglio sarebbe apprezzato.
Come richiesto, ecco il codice che sto utilizzando:
"""
dumpimages.py
Downloads all the images on the supplied URL, and saves them to the
specified output file ("/test/" by default)
Usage:
python dumpimages.py http://example.com/ [output]
"""
from BeautifulSoup import BeautifulSoup as bs
import urlparse
from urllib2 import urlopen
from urllib import urlretrieve
import os
import sys
def main(url, out_folder="C:\asdf\"):
"""Downloads all the images at 'url' to /test/"""
soup = bs(urlopen(url))
parsed = list(urlparse.urlparse(url))
for image in soup.findAll("img"):
print "Image: %(src)s" % image
filename = image["src"].split("/")[-1]
parsed[2] = image["src"]
outpath = os.path.join(out_folder, filename)
if image["src"].lower().startswith("http"):
urlretrieve(image["src"], outpath)
else:
urlretrieve(urlparse.urlunparse(parsed), outpath)
def _usage():
print "usage: python dumpimages.py http://example.com [outpath]"
if __name__ == "__main__":
url = sys.argv[-1]
out_folder = "/test/"
if not url.lower().startswith("http"):
out_folder = sys.argv[-1]
url = sys.argv[-2]
if not url.lower().startswith("http"):
_usage()
sys.exit(-1)
main(url, out_folder)
iniziare con the Python Tutorial: http://docs.python.org/tutorial/interpreter.html –