listing15-2.py

来自「《Beginning Python--From Novice to Profes」· Python 代码 · 共 34 行

PY
34
字号
from urllib import urlopenfrom HTMLParser import HTMLParserclass Scraper(HTMLParser):    in_h3 = False    in_link = False    def handle_starttag(self, tag, attrs):        attrs = dict(attrs)        if tag == 'h3':            self.in_h3 = True        if tag == 'a' and 'href' in attrs:            self.in_link = True            self.chunks = []            self.url = attrs['href']    def handle_data(self, data):        if self.in_link:            self.chunks.append(data)    def handle_endtag(self, tag):        if tag == 'h3':            self.in_h3 = False        if tag == 'a':            if self.in_h3 and self.in_link:                print '%s (%s)' % (''.join(self.chunks), self.url)            self.in_link = Falsetext = urlopen('http://python.org/community/jobs').read()parser = Scraper()parser.feed(text)parser.close()

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?