我到处看过谷歌和它的档案。有好几篇文章,但似乎都帮不了我。所以我想我会来这里找一个更具体的答案。
目标:我想在一个网站上运行this code,以便一次获取所有图片文件。它将节省大量的指向和点击。
我在Windows7x64机器上安装了Python2.3.5。它安装在C:\ Python23中。
可以说,我怎样才能让这个剧本“走”?
============================================================================================
哇哦。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)
在windows平台上,您有两个选择:
在命令行终端中,键入
c:\ python23\python xxxx.py
从菜单中打开python编辑器IDLE,打开xxxx.py,然后按F5运行它。
对于您发布的代码,错误出现在以下行:
应该是:
通常,您可以双击Windows资源管理器中的
.py
文件来运行它。如果不起作用,可以在同一目录中创建包含以下内容的批处理文件:然后双击该批处理文件。或者,当工作目录是脚本的位置时,您可以在命令提示符下运行该行。
既然你看起来是在windows上,你可以这样做
python <filename.py>
。检查python的bin文件夹是否在您的路径中,或者您可以执行c:\python23\bin\python <filename.py>
。Python是一种解释性语言,因此需要解释程序来运行文件,就像需要java运行时来运行jar文件一样。相关问题 更多 >
编程相关推荐