#!/usr/bin/env python # -*- coding: utf-8 -*- import urllib import sgmllib import sys class PortageParser(sgmllib.SGMLParser): def __init__(self, verbose=0): sgmllib.SGMLParser.__init__(self, verbose) self.bullseye = False self.content = [] def parse(self, string): self.feed(string) self.close() def start_div(self, attributes): for name, value in attributes: if name == "id" and value == "search_results": self.bullseye = True def end_div(self): self.bullseye = False def handle_data(self, data): if self.bullseye: self.content = [a for a in self.content if a.strip()] self.content.append(data.strip()) def get_content(self): self.content = self.content[0].split('/') print self.content[0] def run(): if len(sys.argv) == 1: print "Użycie:\n\tpython "+sys.argv[0]+" nazwa_pakietu" else: f = urllib.urlopen("http://gentoo-portage.com/Search?search="+sys.argv[1]) html = f.read() parser = PortageParser() parser.parse(html) parser.get_content() if __name__ == "__main__": run()