html_parsers.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # Copyright 2021 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from html.parser import HTMLParser
  15. from typing import Dict, Iterable, List, Optional, Tuple
  16. class TestHtmlParser(HTMLParser):
  17. """A generic HTML page parser which extracts useful things from the HTML"""
  18. def __init__(self):
  19. super().__init__()
  20. # a list of links found in the doc
  21. self.links: List[str] = []
  22. # the values of any hidden <input>s: map from name to value
  23. self.hiddens: Dict[str, Optional[str]] = {}
  24. # the values of any radio buttons: map from name to list of values
  25. self.radios: Dict[str, List[Optional[str]]] = {}
  26. def handle_starttag(
  27. self, tag: str, attrs: Iterable[Tuple[str, Optional[str]]]
  28. ) -> None:
  29. attr_dict = dict(attrs)
  30. if tag == "a":
  31. href = attr_dict["href"]
  32. if href:
  33. self.links.append(href)
  34. elif tag == "input":
  35. input_name = attr_dict.get("name")
  36. if attr_dict["type"] == "radio":
  37. assert input_name
  38. self.radios.setdefault(input_name, []).append(attr_dict["value"])
  39. elif attr_dict["type"] == "hidden":
  40. assert input_name
  41. self.hiddens[input_name] = attr_dict["value"]
  42. def error(_, message):
  43. raise AssertionError(message)