我正在编写一个给定 http 地址的小程序,它会找到并下载其中的图像。我现在的情况是这样的:
import urllib2, html
class HTMLNode(object):
def __init__(self,tag,attr,content,closed=True):
self.tag = tag
self.attr = attr
self.content = content
self.closed = closed
def istext(self):
return self.tag == '_text_'
def tostring(self):
if self.istext():
return self.content
ret = '<'+self.tag
for k, v in self.attr.items():
ret += ' '+k+'="'+v+'"'
ret += '>'
if self.closed:
for c in self.content:
ret += c.tostring()
ret += '</'+self.tag+'>'
return ret
def find_by_tag(self,tag):
ret = []
if self.tag == tag: ret += [self]
if not self.istext():
for c in self.content:
ret += c.find_by_tag(tag)
return ret
def imagegrab(url):
req = urllib2.Request(url)
response = urllib2.urlopen(req)
output = open(url, 'wb')
output.write(response.read())
缺少的链接正在寻找一种使用 HTMLNode 类的“find_by_tag”功能的方法,以便程序扫描“”标签并从网站下载图片。有人可以帮我吗?