Tôi đã xem toàn bộ Google và các lưu trữ của Google. Có một số bài viết hay, nhưng dường như không có gì giúp tôi. Vì vậy, tôi nghĩ tôi sẽ đến đây để có câu trả lời cụ thể hơn.Python - Làm cách nào để bạn chạy tệp .py?
Mục tiêu: Tôi muốn chạy this code trên trang web để nhận tất cả các tệp hình ảnh cùng một lúc. Nó sẽ tiết kiệm rất nhiều chỉ và nhấp chuột.
Tôi đã có Python 2.3.5 trên máy tính Windows 7 x64. Nó được cài đặt trong C: \ Python23.
Làm cách nào để nhận tập lệnh này "chuyển", vậy nên nói?
=====================================
WOW. 35k lượt xem. Thấy như thế nào điều này là kết quả hàng đầu trên Google, đây là một liên kết hữu ích tôi tìm thấy trong những năm qua:
http://learnpythonthehardway.org/book/ex1.html
Đối với thiết lập, xem tập 0.
========= ============================
FYI: Tôi không có kinh nghiệm với Python. Lời khuyên nào sẽ được đánh giá cao.
Theo yêu cầu, đây là đoạn code tôi đang sử dụng:
"""
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)
Bắt đầu với Hướng dẫn Python: http://docs.python.org/tutorial/interpreter.html –