old_contents: try:
      from urllib.request import urlopen
      from urllib.parse import urljoin
  except ImportError:
      from urllib import urlopen
      from urlparse import urljoin
  import timeit
  
  import html5lib
  import tinycss2
  
  import cssselect2
  
  
  def parse(url):
      root = html5lib.parse(urlopen(url))
      matcher = cssselect2.Matcher()
      for element in root.iter():
          if element.tag == '{http://www.w3.org/1999/xhtml}style':
              rules = tinycss2.parse_stylesheet(element.text)
          elif (element.tag == '{http://www.w3.org/1999/xhtml}link' and
                element.get('rel').strip() == 'stylesheet'):
              rules, _ = tinycss2.parse_stylesheet_bytes(
                  urlopen(urljoin(url, element.get('href'))).read())
          else:
              continue
  
          for rule in rules:
              # Ignore all at-rules
              if rule.type == 'qualified-rule':
                  try:
                      selectors = cssselect2.compile_selector_list(rule.prelude)
                  except cssselect2.SelectorError as error:
                      print('Invalid selector: %s %s'
                            % (tinycss2.serialize(rule.prelude), error))
                  else:
                      for selector in selectors:
                          matcher.add_selector(selector, None)
      return root, matcher
  
  
  def match(root, matcher):
      elements = cssselect2.ElementWrapper.from_html_root(root).iter_subtree()
      for element in elements:
          for _ in matcher.match(element):
              pass
  
  
  if __name__ == '__main__':
      root, matcher = parse('http://dev.w3.org/csswg/selectors4/')
      for t in timeit.repeat(lambda: match(root, matcher), number=3):
          print('%.3f' % t)
  
new_contents: """
  
  In this cssselect2 example:
  - we parse a CSS stylesheet,
  - we parse HTML document and
  - we find the CSS rules matching each HTML tag.
  
  """
  
  from xml.etree import ElementTree
  
  import cssselect2
  import tinycss2
  
  
  # Parse CSS and add rules to the matcher
  
  matcher = cssselect2.Matcher()
  
  rules = tinycss2.parse_stylesheet('''
    body { font-size: 2em }
    body p { background: red }
    p { color: blue }
  ''', skip_whitespace=True)
  
  for rule in rules:
      selectors = cssselect2.compile_selector_list(rule.prelude)
      selector_string = tinycss2.serialize(rule.prelude)
      content_string = tinycss2.serialize(rule.content)
      payload = (selector_string, content_string)
      for selector in selectors:
          matcher.add_selector(selector, payload)
  
  
  # Parse HTML and find CSS rules applying to each tag
  
  html_tree = ElementTree.fromstring('''
    <html>
      <body>
        <p>Test</p>
      </body>
    </html>
  ''')
  wrapper = cssselect2.ElementWrapper.from_html_root(html_tree)
  for element in wrapper.iter_subtree():
      tag = element.etree_element.tag.split('}')[-1]
      print('Found tag "{}" in HTML'.format(tag))
  
      matches = matcher.match(element)
      if matches:
          for match in matches:
              specificity, order, pseudo, payload = match
              selector_string, content_string = payload
              print('Matching selector "{}" ({})'.format(
                  selector_string, content_string))
      else:
          print('No rule matching this tag')
      print()
  
subject: Use a simpler example