我想打印特定网页上的所有url。下面的代码没有错误,但无法在pycharm控制台中看到所需的结果。任何帮助都将不胜感激。在控制台上只出现了"hello"。提前谢谢你。
from sgmllib import SGMLParser
import urllib
class URLLister(SGMLParser):
def reset(self):
SGMLParser.reset(self)
self.urls = []
def start_a(self, attrs):
href = [v for k, v in attrs if k == 'href']
print href
if href:
self.urls.extend(href)
usock = urllib.urlopen("http://diveintopython.org/")
parser = URLLister()
parser.feed(usock.read())
print "hello"
usock.close()
parser.close()
for url in parser.urls:
print url
usock = urllib.urlopen("http://diveintopython.org/")
我怀疑你的意思是…
usock = urllib.urlopen("http://diveintopython.NET/")
…