Python中文
首页
教程
问答
标签
搜索
登录
注册
Python—如何运行.py文件?
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我到处看过谷歌和它的档案。有好几篇文章,但似乎都帮不了我。所以我想我会来这里找一个更具体的答案。</p> <p>目标:我想在一个网站上运行<a href="https://stackoverflow.com/questions/257409/download-image-file-from-the-html-page-source-using-python">this code</a>,以便一次获取所有图片文件。它将节省大量的指向和点击。</p> <p>我在Windows7x64机器上安装了Python2.3.5。它安装在C:\ Python23中。</p> <p>可以说,我怎样才能让这个剧本“走”?</p> <p><strong>============================================================================================</p> <p><strong>哇哦。</strong>35k次。鉴于这是谷歌的头号搜索结果,这里有一个多年来我发现的有用链接:</p> <p><a href="http://learnpythonthehardway.org/book/ex1.html" rel="noreferrer">http://learnpythonthehardway.org/book/ex1.html</a></p> <p>有关设置,请参见练习0。</p> <p><strong>============================================================================================</p> <p>仅供参考:我对Python没有经验。任何建议都将不胜感激。</p> <p>根据要求,这是我使用的代码:</p> <pre><code>""" 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) </code></pre>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>通常,您可以双击Windows资源管理器中的<code>.py</code>文件来运行它。如果不起作用,可以在同一目录中创建包含以下内容的批处理文件:</p> <pre><code>C:\python23\python YOURSCRIPTNAME.py </code></pre> <p>然后双击该批处理文件。或者,当工作目录是脚本的位置时,您可以在命令提示符下运行该行。</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
jupyter运行一个旧的pytorch版本
8 回答
Jupyter运行不同版本的卸载库?
6 回答
Jupyter运行指定的键盘快捷键
4 回答
Jupyter通过.local文件“逃逸”virtualenv。我该如何缓解这种情况?
8 回答
Jupyter重新加载自定义样式
5 回答
Jupyter错误:“没有名为Jupyter_core.paths的模块”
10 回答
jupyter错误:无法在随机林中将决策树视为png
6 回答
Jupyter错误'内核似乎已经死亡,它将自动重新启动'为一个给定的代码块
7 回答
Jupyter错误地用阿拉伯语和字母数字元素显示Python列表
2 回答
Jupyter隐藏数据帧索引,但保留原始样式
10 回答
Jupyter集线器:启动器中出现致命错误。。。系统找不到指定的文件
8 回答
Jupyther中相同值的相同哈希,但导出到Bigquery时不相同
2 回答
Jupy上Python的读/写访问问题
3 回答
jupy上没有模块cv
4 回答
Jupy上的排序错误
8 回答
Jupy中bqplot图形的紧凑布局
1 回答
Jupy中matplotlib plot的连续更新
10 回答
Jupy中Numpy函数的文档
1 回答
Jupy中Pandas的自动完成问题
7 回答
jupy中Qt后端的Matplotlib动画
4 回答