⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 listing15-2.py

📁 《Beginning Python--From Novice to Professional》 的源码
💻 PY
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -