2009-11-12 31 views
1
from HTMLParser import HTMLParser 

from urllib import urlopen 

class Spider(HTMLParser): 

     def __init__(self, url): 
       HTMLParser.__init__(self) 
       req = urlopen(url) 
       self.feed(req.read()) 

     def handle_starttag(self, tag, attrs): 
       if tag == 'a' and attrs: 
         print "Found link => %s" % attrs[0][1] 

Spider('http://stackoverflow.com/questions/tagged/python') 
+0

我该如何在一个脚本中做到这一点? – 2009-11-12 14:40:45

回答

3
python spider.py > output.html 
+0

很好的答案!太糟糕了,我只能举一次:-) – Abgan 2009-11-12 14:02:39

0

我还没有与蜘蛛搞砸可言,但它打印HTML,或者你只是打印“找到链接...”行?如果你只是印刷那些,你可以做一些像outfl = open('output.txt')

然后,而不是print,请致电outfl.write("Found link => %s" % attrs[0][1])

如果您需要HTML格式,您可以随时在之前写出<html><head></head><body>,之后写出</body></html>。此外,使用outfl = open('output.html')而不是.txt作为文件名。

我完全错过了这里的问题吗?如果你想要更好的答案,你应该更好地描述这个问题。

1

把这个在你的脚本的顶部:

import sys 
sys.stdout = file('output.html', 'w') 

这一切重定向你的脚本写到标准输出(其中包括print语句)的文件“output.html”。

相关问题