3
我想写一个python脚本,从webpage.on网页下载图片(我正在使用美国国家航空航天局的当天页面图片),每天发布一张新图片,以不同的文件名。从网页下载图片使用python
所以我的解决方案是使用HTMLParser解析HTML,查找“jpg”,并将图像的路径和文件名写入HTML解析器对象的属性(命名为“output”,请参阅下面的代码) 。
我是python和OOP的新手(这是我的第一个真正的python脚本),所以我不确定这是如何通常完成的。任何建议和指针是受欢迎的。
这里是我的代码:
# Grab image url
response = urllib2.urlopen('http://apod.nasa.gov/apod/astropix.html')
html = response.read()
class MyHTMLParser(HTMLParser):
def handle_starttag(self, tag, attrs):
# Only parse the 'anchor' tag.
if tag == "a":
# Check the list of defined attributes.
for name, value in attrs:
# If href is defined, print it.
if name == "href":
if value[len(value)-3:len(value)]=="jpg":
#print value
self.output=value #return the path+file name of the image
parser = MyHTMLParser()
parser.feed(html)
imgurl='http://apod.nasa.gov/apod/'+parser.output
是什么问题? – piokuc 2013-03-11 23:19:51
如果您的代码正在运行,并且您只想对可能的改进方法发表评论,则可以咨询Code Review的优秀人员:http://codereview.stackexchange.com/ – bernie 2013-03-11 23:25:32
...我不知道codereview是否存在...感谢 – Cici 2013-03-11 23:32:50