我瀏覽了Google及其檔案。有幾篇很好的文章,但似乎沒有人能幫助我。所以我想我會來這裏作一個更具體的答案。Python - 你如何運行.py文件?
目標:我想在網站上運行this code以一次獲取所有圖片文件。它會節省大量的指點和點擊。
我在Windows 7 x64機器上安裝了Python 2.3.5。它安裝在C:\ Python23中。
如何讓這個腳本「走」,可以這麼說?
=====================================
WOW。 35k次瀏覽。看到如何,這是在谷歌上面的結果,這裏是一個有用的鏈接,我發現這些年來:
http://learnpythonthehardway.org/book/ex1.html
有關設置,請參閱練習0
========= ============================
僅供參考:我對Python沒有經驗。任何意見,將不勝感激。
按照要求,這裏是我使用的代碼:
"""
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)
開始與Python教程:http://docs.python.org/tutorial/interpreter.html – 2012-02-29 03:16:25