Guest User

Parser

a guest
Feb 10th, 2016
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.64 KB | None | 0 0
  1. #!/usr/local/bin/python3
  2. # -*- coding: utf-8 -*-
  3.  
  4. from html.parser import HTMLParser
  5. from urllib.request import quote, urlopen
  6.  
  7.  
  8. class Parser(HTMLParser):
  9.     """Helper class for retrieving header."""
  10.  
  11.     def __init__(self):
  12.         HTMLParser.__init__(self)
  13.         self.in_head_tag = False
  14.         self.in_span_tag = False
  15.         self.head_text = ''
  16.  
  17.     def handle_starttag(self, tag, attrs):
  18.         if tag == 'h1':
  19.             attrs = dict(attrs)
  20.             if 'id' in attrs and attrs['id'] == 'firstHeading':
  21.                 self.in_head_tag = True
  22.         if self.in_head_tag and tag == 'span':
  23.             self.in_span_tag = True
  24.  
  25.     def handle_endtag(self, tag):
  26.         if self.in_head_tag and tag == 'h1':
  27.             self.in_head_tag = False
  28.         if self.in_span_tag and tag == 'span':
  29.             self.in_span_tag = False
  30.  
  31.     def handle_data(self, data):
  32.         if self.in_span_tag:
  33.             self.head_text = data
  34.  
  35.     def parse(self, page):
  36.         """Return text of article."""
  37.  
  38.         self.feed(page)
  39.         return self.head_text
  40.  
  41.  
  42. WIKI_ROOT = 'http://wiki.cs.hse.ru/'
  43.  
  44. def load(name):
  45.     """Return text of the article with specified url."""
  46.  
  47.     with urlopen(WIKI_ROOT + quote(name)) as file:
  48.         page = str(file.read(), encoding='utf-8')
  49.     return page
  50.  
  51.  
  52. def get_head(page):
  53.     """Return head of page."""
  54.  
  55.     return Parser().parse(page)
  56.  
  57.  
  58. def main():
  59.     page = load('Рекомендательная_система_(семинар)')
  60.     print('Заголовок: "{head}"'.format(head = get_head(page)))
  61.  
  62.  
  63. if __name__ == '__main__':
  64.     main()
Advertisement
Add Comment
Please, Sign In to add comment