html.py 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. """
  2. :mod:`pandas.io.html` is a module containing functionality for dealing with
  3. HTML IO.
  4. """
  5. from __future__ import annotations
  6. from collections import abc
  7. import numbers
  8. import os
  9. import re
  10. from typing import (
  11. Pattern,
  12. Sequence,
  13. )
  14. from pandas._typing import FilePathOrBuffer
  15. from pandas.compat._optional import import_optional_dependency
  16. from pandas.errors import (
  17. AbstractMethodError,
  18. EmptyDataError,
  19. )
  20. from pandas.util._decorators import deprecate_nonkeyword_arguments
  21. from pandas.core.dtypes.common import is_list_like
  22. from pandas.core.construction import create_series_with_explicit_dtype
  23. from pandas.core.frame import DataFrame
  24. from pandas.io.common import (
  25. is_url,
  26. stringify_path,
  27. urlopen,
  28. validate_header_arg,
  29. )
  30. from pandas.io.formats.printing import pprint_thing
  31. from pandas.io.parsers import TextParser
  32. _IMPORTS = False
  33. _HAS_BS4 = False
  34. _HAS_LXML = False
  35. _HAS_HTML5LIB = False
  36. def _importers():
  37. # import things we need
  38. # but make this done on a first use basis
  39. global _IMPORTS
  40. if _IMPORTS:
  41. return
  42. global _HAS_BS4, _HAS_LXML, _HAS_HTML5LIB
  43. bs4 = import_optional_dependency("bs4", errors="ignore")
  44. _HAS_BS4 = bs4 is not None
  45. lxml = import_optional_dependency("lxml.etree", errors="ignore")
  46. _HAS_LXML = lxml is not None
  47. html5lib = import_optional_dependency("html5lib", errors="ignore")
  48. _HAS_HTML5LIB = html5lib is not None
  49. _IMPORTS = True
  50. #############
  51. # READ HTML #
  52. #############
  53. _RE_WHITESPACE = re.compile(r"[\r\n]+|\s{2,}")
  54. def _remove_whitespace(s: str, regex=_RE_WHITESPACE) -> str:
  55. """
  56. Replace extra whitespace inside of a string with a single space.
  57. Parameters
  58. ----------
  59. s : str or unicode
  60. The string from which to remove extra whitespace.
  61. regex : re.Pattern
  62. The regular expression to use to remove extra whitespace.
  63. Returns
  64. -------
  65. subd : str or unicode
  66. `s` with all extra whitespace replaced with a single space.
  67. """
  68. return regex.sub(" ", s.strip())
  69. def _get_skiprows(skiprows):
  70. """
  71. Get an iterator given an integer, slice or container.
  72. Parameters
  73. ----------
  74. skiprows : int, slice, container
  75. The iterator to use to skip rows; can also be a slice.
  76. Raises
  77. ------
  78. TypeError
  79. * If `skiprows` is not a slice, integer, or Container
  80. Returns
  81. -------
  82. it : iterable
  83. A proper iterator to use to skip rows of a DataFrame.
  84. """
  85. if isinstance(skiprows, slice):
  86. start, step = skiprows.start or 0, skiprows.step or 1
  87. return list(range(start, skiprows.stop, step))
  88. elif isinstance(skiprows, numbers.Integral) or is_list_like(skiprows):
  89. return skiprows
  90. elif skiprows is None:
  91. return 0
  92. raise TypeError(f"{type(skiprows).__name__} is not a valid type for skipping rows")
  93. def _read(obj):
  94. """
  95. Try to read from a url, file or string.
  96. Parameters
  97. ----------
  98. obj : str, unicode, or file-like
  99. Returns
  100. -------
  101. raw_text : str
  102. """
  103. if is_url(obj):
  104. with urlopen(obj) as url:
  105. text = url.read()
  106. elif hasattr(obj, "read"):
  107. text = obj.read()
  108. elif isinstance(obj, (str, bytes)):
  109. text = obj
  110. try:
  111. if os.path.isfile(text):
  112. with open(text, "rb") as f:
  113. return f.read()
  114. except (TypeError, ValueError):
  115. pass
  116. else:
  117. raise TypeError(f"Cannot read object of type '{type(obj).__name__}'")
  118. return text
  119. class _HtmlFrameParser:
  120. """
  121. Base class for parsers that parse HTML into DataFrames.
  122. Parameters
  123. ----------
  124. io : str or file-like
  125. This can be either a string of raw HTML, a valid URL using the HTTP,
  126. FTP, or FILE protocols or a file-like object.
  127. match : str or regex
  128. The text to match in the document.
  129. attrs : dict
  130. List of HTML <table> element attributes to match.
  131. encoding : str
  132. Encoding to be used by parser
  133. displayed_only : bool
  134. Whether or not items with "display:none" should be ignored
  135. Attributes
  136. ----------
  137. io : str or file-like
  138. raw HTML, URL, or file-like object
  139. match : regex
  140. The text to match in the raw HTML
  141. attrs : dict-like
  142. A dictionary of valid table attributes to use to search for table
  143. elements.
  144. encoding : str
  145. Encoding to be used by parser
  146. displayed_only : bool
  147. Whether or not items with "display:none" should be ignored
  148. Notes
  149. -----
  150. To subclass this class effectively you must override the following methods:
  151. * :func:`_build_doc`
  152. * :func:`_attr_getter`
  153. * :func:`_text_getter`
  154. * :func:`_parse_td`
  155. * :func:`_parse_thead_tr`
  156. * :func:`_parse_tbody_tr`
  157. * :func:`_parse_tfoot_tr`
  158. * :func:`_parse_tables`
  159. * :func:`_equals_tag`
  160. See each method's respective documentation for details on their
  161. functionality.
  162. """
  163. def __init__(self, io, match, attrs, encoding, displayed_only):
  164. self.io = io
  165. self.match = match
  166. self.attrs = attrs
  167. self.encoding = encoding
  168. self.displayed_only = displayed_only
  169. def parse_tables(self):
  170. """
  171. Parse and return all tables from the DOM.
  172. Returns
  173. -------
  174. list of parsed (header, body, footer) tuples from tables.
  175. """
  176. tables = self._parse_tables(self._build_doc(), self.match, self.attrs)
  177. return (self._parse_thead_tbody_tfoot(table) for table in tables)
  178. def _attr_getter(self, obj, attr):
  179. """
  180. Return the attribute value of an individual DOM node.
  181. Parameters
  182. ----------
  183. obj : node-like
  184. A DOM node.
  185. attr : str or unicode
  186. The attribute, such as "colspan"
  187. Returns
  188. -------
  189. str or unicode
  190. The attribute value.
  191. """
  192. # Both lxml and BeautifulSoup have the same implementation:
  193. return obj.get(attr)
  194. def _text_getter(self, obj):
  195. """
  196. Return the text of an individual DOM node.
  197. Parameters
  198. ----------
  199. obj : node-like
  200. A DOM node.
  201. Returns
  202. -------
  203. text : str or unicode
  204. The text from an individual DOM node.
  205. """
  206. raise AbstractMethodError(self)
  207. def _parse_td(self, obj):
  208. """
  209. Return the td elements from a row element.
  210. Parameters
  211. ----------
  212. obj : node-like
  213. A DOM <tr> node.
  214. Returns
  215. -------
  216. list of node-like
  217. These are the elements of each row, i.e., the columns.
  218. """
  219. raise AbstractMethodError(self)
  220. def _parse_thead_tr(self, table):
  221. """
  222. Return the list of thead row elements from the parsed table element.
  223. Parameters
  224. ----------
  225. table : a table element that contains zero or more thead elements.
  226. Returns
  227. -------
  228. list of node-like
  229. These are the <tr> row elements of a table.
  230. """
  231. raise AbstractMethodError(self)
  232. def _parse_tbody_tr(self, table):
  233. """
  234. Return the list of tbody row elements from the parsed table element.
  235. HTML5 table bodies consist of either 0 or more <tbody> elements (which
  236. only contain <tr> elements) or 0 or more <tr> elements. This method
  237. checks for both structures.
  238. Parameters
  239. ----------
  240. table : a table element that contains row elements.
  241. Returns
  242. -------
  243. list of node-like
  244. These are the <tr> row elements of a table.
  245. """
  246. raise AbstractMethodError(self)
  247. def _parse_tfoot_tr(self, table):
  248. """
  249. Return the list of tfoot row elements from the parsed table element.
  250. Parameters
  251. ----------
  252. table : a table element that contains row elements.
  253. Returns
  254. -------
  255. list of node-like
  256. These are the <tr> row elements of a table.
  257. """
  258. raise AbstractMethodError(self)
  259. def _parse_tables(self, doc, match, attrs):
  260. """
  261. Return all tables from the parsed DOM.
  262. Parameters
  263. ----------
  264. doc : the DOM from which to parse the table element.
  265. match : str or regular expression
  266. The text to search for in the DOM tree.
  267. attrs : dict
  268. A dictionary of table attributes that can be used to disambiguate
  269. multiple tables on a page.
  270. Raises
  271. ------
  272. ValueError : `match` does not match any text in the document.
  273. Returns
  274. -------
  275. list of node-like
  276. HTML <table> elements to be parsed into raw data.
  277. """
  278. raise AbstractMethodError(self)
  279. def _equals_tag(self, obj, tag):
  280. """
  281. Return whether an individual DOM node matches a tag
  282. Parameters
  283. ----------
  284. obj : node-like
  285. A DOM node.
  286. tag : str
  287. Tag name to be checked for equality.
  288. Returns
  289. -------
  290. boolean
  291. Whether `obj`'s tag name is `tag`
  292. """
  293. raise AbstractMethodError(self)
  294. def _build_doc(self):
  295. """
  296. Return a tree-like object that can be used to iterate over the DOM.
  297. Returns
  298. -------
  299. node-like
  300. The DOM from which to parse the table element.
  301. """
  302. raise AbstractMethodError(self)
  303. def _parse_thead_tbody_tfoot(self, table_html):
  304. """
  305. Given a table, return parsed header, body, and foot.
  306. Parameters
  307. ----------
  308. table_html : node-like
  309. Returns
  310. -------
  311. tuple of (header, body, footer), each a list of list-of-text rows.
  312. Notes
  313. -----
  314. Header and body are lists-of-lists. Top level list is a list of
  315. rows. Each row is a list of str text.
  316. Logic: Use <thead>, <tbody>, <tfoot> elements to identify
  317. header, body, and footer, otherwise:
  318. - Put all rows into body
  319. - Move rows from top of body to header only if
  320. all elements inside row are <th>
  321. - Move rows from bottom of body to footer only if
  322. all elements inside row are <th>
  323. """
  324. header_rows = self._parse_thead_tr(table_html)
  325. body_rows = self._parse_tbody_tr(table_html)
  326. footer_rows = self._parse_tfoot_tr(table_html)
  327. def row_is_all_th(row):
  328. return all(self._equals_tag(t, "th") for t in self._parse_td(row))
  329. if not header_rows:
  330. # The table has no <thead>. Move the top all-<th> rows from
  331. # body_rows to header_rows. (This is a common case because many
  332. # tables in the wild have no <thead> or <tfoot>
  333. while body_rows and row_is_all_th(body_rows[0]):
  334. header_rows.append(body_rows.pop(0))
  335. header = self._expand_colspan_rowspan(header_rows)
  336. body = self._expand_colspan_rowspan(body_rows)
  337. footer = self._expand_colspan_rowspan(footer_rows)
  338. return header, body, footer
  339. def _expand_colspan_rowspan(self, rows):
  340. """
  341. Given a list of <tr>s, return a list of text rows.
  342. Parameters
  343. ----------
  344. rows : list of node-like
  345. List of <tr>s
  346. Returns
  347. -------
  348. list of list
  349. Each returned row is a list of str text.
  350. Notes
  351. -----
  352. Any cell with ``rowspan`` or ``colspan`` will have its contents copied
  353. to subsequent cells.
  354. """
  355. all_texts = [] # list of rows, each a list of str
  356. remainder: list[tuple[int, str, int]] = [] # list of (index, text, nrows)
  357. for tr in rows:
  358. texts = [] # the output for this row
  359. next_remainder = []
  360. index = 0
  361. tds = self._parse_td(tr)
  362. for td in tds:
  363. # Append texts from previous rows with rowspan>1 that come
  364. # before this <td>
  365. while remainder and remainder[0][0] <= index:
  366. prev_i, prev_text, prev_rowspan = remainder.pop(0)
  367. texts.append(prev_text)
  368. if prev_rowspan > 1:
  369. next_remainder.append((prev_i, prev_text, prev_rowspan - 1))
  370. index += 1
  371. # Append the text from this <td>, colspan times
  372. text = _remove_whitespace(self._text_getter(td))
  373. rowspan = int(self._attr_getter(td, "rowspan") or 1)
  374. colspan = int(self._attr_getter(td, "colspan") or 1)
  375. for _ in range(colspan):
  376. texts.append(text)
  377. if rowspan > 1:
  378. next_remainder.append((index, text, rowspan - 1))
  379. index += 1
  380. # Append texts from previous rows at the final position
  381. for prev_i, prev_text, prev_rowspan in remainder:
  382. texts.append(prev_text)
  383. if prev_rowspan > 1:
  384. next_remainder.append((prev_i, prev_text, prev_rowspan - 1))
  385. all_texts.append(texts)
  386. remainder = next_remainder
  387. # Append rows that only appear because the previous row had non-1
  388. # rowspan
  389. while remainder:
  390. next_remainder = []
  391. texts = []
  392. for prev_i, prev_text, prev_rowspan in remainder:
  393. texts.append(prev_text)
  394. if prev_rowspan > 1:
  395. next_remainder.append((prev_i, prev_text, prev_rowspan - 1))
  396. all_texts.append(texts)
  397. remainder = next_remainder
  398. return all_texts
  399. def _handle_hidden_tables(self, tbl_list, attr_name):
  400. """
  401. Return list of tables, potentially removing hidden elements
  402. Parameters
  403. ----------
  404. tbl_list : list of node-like
  405. Type of list elements will vary depending upon parser used
  406. attr_name : str
  407. Name of the accessor for retrieving HTML attributes
  408. Returns
  409. -------
  410. list of node-like
  411. Return type matches `tbl_list`
  412. """
  413. if not self.displayed_only:
  414. return tbl_list
  415. return [
  416. x
  417. for x in tbl_list
  418. if "display:none"
  419. not in getattr(x, attr_name).get("style", "").replace(" ", "")
  420. ]
  421. class _BeautifulSoupHtml5LibFrameParser(_HtmlFrameParser):
  422. """
  423. HTML to DataFrame parser that uses BeautifulSoup under the hood.
  424. See Also
  425. --------
  426. pandas.io.html._HtmlFrameParser
  427. pandas.io.html._LxmlFrameParser
  428. Notes
  429. -----
  430. Documentation strings for this class are in the base class
  431. :class:`pandas.io.html._HtmlFrameParser`.
  432. """
  433. def __init__(self, *args, **kwargs):
  434. super().__init__(*args, **kwargs)
  435. from bs4 import SoupStrainer
  436. self._strainer = SoupStrainer("table")
  437. def _parse_tables(self, doc, match, attrs):
  438. element_name = self._strainer.name
  439. tables = doc.find_all(element_name, attrs=attrs)
  440. if not tables:
  441. raise ValueError("No tables found")
  442. result = []
  443. unique_tables = set()
  444. tables = self._handle_hidden_tables(tables, "attrs")
  445. for table in tables:
  446. if self.displayed_only:
  447. for elem in table.find_all(style=re.compile(r"display:\s*none")):
  448. elem.decompose()
  449. if table not in unique_tables and table.find(text=match) is not None:
  450. result.append(table)
  451. unique_tables.add(table)
  452. if not result:
  453. raise ValueError(f"No tables found matching pattern {repr(match.pattern)}")
  454. return result
  455. def _text_getter(self, obj):
  456. return obj.text
  457. def _equals_tag(self, obj, tag):
  458. return obj.name == tag
  459. def _parse_td(self, row):
  460. return row.find_all(("td", "th"), recursive=False)
  461. def _parse_thead_tr(self, table):
  462. return table.select("thead tr")
  463. def _parse_tbody_tr(self, table):
  464. from_tbody = table.select("tbody tr")
  465. from_root = table.find_all("tr", recursive=False)
  466. # HTML spec: at most one of these lists has content
  467. return from_tbody + from_root
  468. def _parse_tfoot_tr(self, table):
  469. return table.select("tfoot tr")
  470. def _setup_build_doc(self):
  471. raw_text = _read(self.io)
  472. if not raw_text:
  473. raise ValueError(f"No text parsed from document: {self.io}")
  474. return raw_text
  475. def _build_doc(self):
  476. from bs4 import BeautifulSoup
  477. bdoc = self._setup_build_doc()
  478. if isinstance(bdoc, bytes) and self.encoding is not None:
  479. udoc = bdoc.decode(self.encoding)
  480. from_encoding = None
  481. else:
  482. udoc = bdoc
  483. from_encoding = self.encoding
  484. return BeautifulSoup(udoc, features="html5lib", from_encoding=from_encoding)
  485. def _build_xpath_expr(attrs) -> str:
  486. """
  487. Build an xpath expression to simulate bs4's ability to pass in kwargs to
  488. search for attributes when using the lxml parser.
  489. Parameters
  490. ----------
  491. attrs : dict
  492. A dict of HTML attributes. These are NOT checked for validity.
  493. Returns
  494. -------
  495. expr : unicode
  496. An XPath expression that checks for the given HTML attributes.
  497. """
  498. # give class attribute as class_ because class is a python keyword
  499. if "class_" in attrs:
  500. attrs["class"] = attrs.pop("class_")
  501. s = " and ".join(f"@{k}={repr(v)}" for k, v in attrs.items())
  502. return f"[{s}]"
  503. _re_namespace = {"re": "http://exslt.org/regular-expressions"}
  504. _valid_schemes = "http", "file", "ftp"
  505. class _LxmlFrameParser(_HtmlFrameParser):
  506. """
  507. HTML to DataFrame parser that uses lxml under the hood.
  508. Warning
  509. -------
  510. This parser can only handle HTTP, FTP, and FILE urls.
  511. See Also
  512. --------
  513. _HtmlFrameParser
  514. _BeautifulSoupLxmlFrameParser
  515. Notes
  516. -----
  517. Documentation strings for this class are in the base class
  518. :class:`_HtmlFrameParser`.
  519. """
  520. def __init__(self, *args, **kwargs):
  521. super().__init__(*args, **kwargs)
  522. def _text_getter(self, obj):
  523. return obj.text_content()
  524. def _parse_td(self, row):
  525. # Look for direct children only: the "row" element here may be a
  526. # <thead> or <tfoot> (see _parse_thead_tr).
  527. return row.xpath("./td|./th")
  528. def _parse_tables(self, doc, match, kwargs):
  529. pattern = match.pattern
  530. # 1. check all descendants for the given pattern and only search tables
  531. # 2. go up the tree until we find a table
  532. xpath_expr = f"//table//*[re:test(text(), {repr(pattern)})]/ancestor::table"
  533. # if any table attributes were given build an xpath expression to
  534. # search for them
  535. if kwargs:
  536. xpath_expr += _build_xpath_expr(kwargs)
  537. tables = doc.xpath(xpath_expr, namespaces=_re_namespace)
  538. tables = self._handle_hidden_tables(tables, "attrib")
  539. if self.displayed_only:
  540. for table in tables:
  541. # lxml utilizes XPATH 1.0 which does not have regex
  542. # support. As a result, we find all elements with a style
  543. # attribute and iterate them to check for display:none
  544. for elem in table.xpath(".//*[@style]"):
  545. if "display:none" in elem.attrib.get("style", "").replace(" ", ""):
  546. elem.getparent().remove(elem)
  547. if not tables:
  548. raise ValueError(f"No tables found matching regex {repr(pattern)}")
  549. return tables
  550. def _equals_tag(self, obj, tag):
  551. return obj.tag == tag
  552. def _build_doc(self):
  553. """
  554. Raises
  555. ------
  556. ValueError
  557. * If a URL that lxml cannot parse is passed.
  558. Exception
  559. * Any other ``Exception`` thrown. For example, trying to parse a
  560. URL that is syntactically correct on a machine with no internet
  561. connection will fail.
  562. See Also
  563. --------
  564. pandas.io.html._HtmlFrameParser._build_doc
  565. """
  566. from lxml.etree import XMLSyntaxError
  567. from lxml.html import (
  568. HTMLParser,
  569. fromstring,
  570. parse,
  571. )
  572. parser = HTMLParser(recover=True, encoding=self.encoding)
  573. try:
  574. if is_url(self.io):
  575. with urlopen(self.io) as f:
  576. r = parse(f, parser=parser)
  577. else:
  578. # try to parse the input in the simplest way
  579. r = parse(self.io, parser=parser)
  580. try:
  581. r = r.getroot()
  582. except AttributeError:
  583. pass
  584. except (UnicodeDecodeError, OSError) as e:
  585. # if the input is a blob of html goop
  586. if not is_url(self.io):
  587. r = fromstring(self.io, parser=parser)
  588. try:
  589. r = r.getroot()
  590. except AttributeError:
  591. pass
  592. else:
  593. raise e
  594. else:
  595. if not hasattr(r, "text_content"):
  596. raise XMLSyntaxError("no text parsed from document", 0, 0, 0)
  597. return r
  598. def _parse_thead_tr(self, table):
  599. rows = []
  600. for thead in table.xpath(".//thead"):
  601. rows.extend(thead.xpath("./tr"))
  602. # HACK: lxml does not clean up the clearly-erroneous
  603. # <thead><th>foo</th><th>bar</th></thead>. (Missing <tr>). Add
  604. # the <thead> and _pretend_ it's a <tr>; _parse_td() will find its
  605. # children as though it's a <tr>.
  606. #
  607. # Better solution would be to use html5lib.
  608. elements_at_root = thead.xpath("./td|./th")
  609. if elements_at_root:
  610. rows.append(thead)
  611. return rows
  612. def _parse_tbody_tr(self, table):
  613. from_tbody = table.xpath(".//tbody//tr")
  614. from_root = table.xpath("./tr")
  615. # HTML spec: at most one of these lists has content
  616. return from_tbody + from_root
  617. def _parse_tfoot_tr(self, table):
  618. return table.xpath(".//tfoot//tr")
  619. def _expand_elements(body):
  620. data = [len(elem) for elem in body]
  621. lens = create_series_with_explicit_dtype(data, dtype_if_empty=object)
  622. lens_max = lens.max()
  623. not_max = lens[lens != lens_max]
  624. empty = [""]
  625. for ind, length in not_max.items():
  626. body[ind] += empty * (lens_max - length)
  627. def _data_to_frame(**kwargs):
  628. head, body, foot = kwargs.pop("data")
  629. header = kwargs.pop("header")
  630. kwargs["skiprows"] = _get_skiprows(kwargs["skiprows"])
  631. if head:
  632. body = head + body
  633. # Infer header when there is a <thead> or top <th>-only rows
  634. if header is None:
  635. if len(head) == 1:
  636. header = 0
  637. else:
  638. # ignore all-empty-text rows
  639. header = [i for i, row in enumerate(head) if any(text for text in row)]
  640. if foot:
  641. body += foot
  642. # fill out elements of body that are "ragged"
  643. _expand_elements(body)
  644. with TextParser(body, header=header, **kwargs) as tp:
  645. return tp.read()
  646. _valid_parsers = {
  647. "lxml": _LxmlFrameParser,
  648. None: _LxmlFrameParser,
  649. "html5lib": _BeautifulSoupHtml5LibFrameParser,
  650. "bs4": _BeautifulSoupHtml5LibFrameParser,
  651. }
  652. def _parser_dispatch(flavor):
  653. """
  654. Choose the parser based on the input flavor.
  655. Parameters
  656. ----------
  657. flavor : str
  658. The type of parser to use. This must be a valid backend.
  659. Returns
  660. -------
  661. cls : _HtmlFrameParser subclass
  662. The parser class based on the requested input flavor.
  663. Raises
  664. ------
  665. ValueError
  666. * If `flavor` is not a valid backend.
  667. ImportError
  668. * If you do not have the requested `flavor`
  669. """
  670. valid_parsers = list(_valid_parsers.keys())
  671. if flavor not in valid_parsers:
  672. raise ValueError(
  673. f"{repr(flavor)} is not a valid flavor, valid flavors are {valid_parsers}"
  674. )
  675. if flavor in ("bs4", "html5lib"):
  676. if not _HAS_HTML5LIB:
  677. raise ImportError("html5lib not found, please install it")
  678. if not _HAS_BS4:
  679. raise ImportError("BeautifulSoup4 (bs4) not found, please install it")
  680. # Although we call this above, we want to raise here right before use.
  681. bs4 = import_optional_dependency("bs4") # noqa:F841
  682. else:
  683. if not _HAS_LXML:
  684. raise ImportError("lxml not found, please install it")
  685. return _valid_parsers[flavor]
  686. def _print_as_set(s) -> str:
  687. arg = ", ".join(pprint_thing(el) for el in s)
  688. return f"{{{arg}}}"
  689. def _validate_flavor(flavor):
  690. if flavor is None:
  691. flavor = "lxml", "bs4"
  692. elif isinstance(flavor, str):
  693. flavor = (flavor,)
  694. elif isinstance(flavor, abc.Iterable):
  695. if not all(isinstance(flav, str) for flav in flavor):
  696. raise TypeError(
  697. f"Object of type {repr(type(flavor).__name__)} "
  698. f"is not an iterable of strings"
  699. )
  700. else:
  701. msg = repr(flavor) if isinstance(flavor, str) else str(flavor)
  702. msg += " is not a valid flavor"
  703. raise ValueError(msg)
  704. flavor = tuple(flavor)
  705. valid_flavors = set(_valid_parsers)
  706. flavor_set = set(flavor)
  707. if not flavor_set & valid_flavors:
  708. raise ValueError(
  709. f"{_print_as_set(flavor_set)} is not a valid set of flavors, valid "
  710. f"flavors are {_print_as_set(valid_flavors)}"
  711. )
  712. return flavor
  713. def _parse(flavor, io, match, attrs, encoding, displayed_only, **kwargs):
  714. flavor = _validate_flavor(flavor)
  715. compiled_match = re.compile(match) # you can pass a compiled regex here
  716. retained = None
  717. for flav in flavor:
  718. parser = _parser_dispatch(flav)
  719. p = parser(io, compiled_match, attrs, encoding, displayed_only)
  720. try:
  721. tables = p.parse_tables()
  722. except ValueError as caught:
  723. # if `io` is an io-like object, check if it's seekable
  724. # and try to rewind it before trying the next parser
  725. if hasattr(io, "seekable") and io.seekable():
  726. io.seek(0)
  727. elif hasattr(io, "seekable") and not io.seekable():
  728. # if we couldn't rewind it, let the user know
  729. raise ValueError(
  730. f"The flavor {flav} failed to parse your input. "
  731. "Since you passed a non-rewindable file "
  732. "object, we can't rewind it to try "
  733. "another parser. Try read_html() with a different flavor."
  734. ) from caught
  735. retained = caught
  736. else:
  737. break
  738. else:
  739. assert retained is not None # for mypy
  740. raise retained
  741. ret = []
  742. for table in tables:
  743. try:
  744. ret.append(_data_to_frame(data=table, **kwargs))
  745. except EmptyDataError: # empty table
  746. continue
  747. return ret
  748. @deprecate_nonkeyword_arguments(version="2.0")
  749. def read_html(
  750. io: FilePathOrBuffer,
  751. match: str | Pattern = ".+",
  752. flavor: str | None = None,
  753. header: int | Sequence[int] | None = None,
  754. index_col: int | Sequence[int] | None = None,
  755. skiprows: int | Sequence[int] | slice | None = None,
  756. attrs: dict[str, str] | None = None,
  757. parse_dates: bool = False,
  758. thousands: str | None = ",",
  759. encoding: str | None = None,
  760. decimal: str = ".",
  761. converters: dict | None = None,
  762. na_values=None,
  763. keep_default_na: bool = True,
  764. displayed_only: bool = True,
  765. ) -> list[DataFrame]:
  766. r"""
  767. Read HTML tables into a ``list`` of ``DataFrame`` objects.
  768. Parameters
  769. ----------
  770. io : str, path object or file-like object
  771. A URL, a file-like object, or a raw string containing HTML. Note that
  772. lxml only accepts the http, ftp and file url protocols. If you have a
  773. URL that starts with ``'https'`` you might try removing the ``'s'``.
  774. match : str or compiled regular expression, optional
  775. The set of tables containing text matching this regex or string will be
  776. returned. Unless the HTML is extremely simple you will probably need to
  777. pass a non-empty string here. Defaults to '.+' (match any non-empty
  778. string). The default value will return all tables contained on a page.
  779. This value is converted to a regular expression so that there is
  780. consistent behavior between Beautiful Soup and lxml.
  781. flavor : str, optional
  782. The parsing engine to use. 'bs4' and 'html5lib' are synonymous with
  783. each other, they are both there for backwards compatibility. The
  784. default of ``None`` tries to use ``lxml`` to parse and if that fails it
  785. falls back on ``bs4`` + ``html5lib``.
  786. header : int or list-like, optional
  787. The row (or list of rows for a :class:`~pandas.MultiIndex`) to use to
  788. make the columns headers.
  789. index_col : int or list-like, optional
  790. The column (or list of columns) to use to create the index.
  791. skiprows : int, list-like or slice, optional
  792. Number of rows to skip after parsing the column integer. 0-based. If a
  793. sequence of integers or a slice is given, will skip the rows indexed by
  794. that sequence. Note that a single element sequence means 'skip the nth
  795. row' whereas an integer means 'skip n rows'.
  796. attrs : dict, optional
  797. This is a dictionary of attributes that you can pass to use to identify
  798. the table in the HTML. These are not checked for validity before being
  799. passed to lxml or Beautiful Soup. However, these attributes must be
  800. valid HTML table attributes to work correctly. For example, ::
  801. attrs = {'id': 'table'}
  802. is a valid attribute dictionary because the 'id' HTML tag attribute is
  803. a valid HTML attribute for *any* HTML tag as per `this document
  804. <https://html.spec.whatwg.org/multipage/dom.html#global-attributes>`__. ::
  805. attrs = {'asdf': 'table'}
  806. is *not* a valid attribute dictionary because 'asdf' is not a valid
  807. HTML attribute even if it is a valid XML attribute. Valid HTML 4.01
  808. table attributes can be found `here
  809. <http://www.w3.org/TR/REC-html40/struct/tables.html#h-11.2>`__. A
  810. working draft of the HTML 5 spec can be found `here
  811. <https://html.spec.whatwg.org/multipage/tables.html>`__. It contains the
  812. latest information on table attributes for the modern web.
  813. parse_dates : bool, optional
  814. See :func:`~read_csv` for more details.
  815. thousands : str, optional
  816. Separator to use to parse thousands. Defaults to ``','``.
  817. encoding : str, optional
  818. The encoding used to decode the web page. Defaults to ``None``.``None``
  819. preserves the previous encoding behavior, which depends on the
  820. underlying parser library (e.g., the parser library will try to use
  821. the encoding provided by the document).
  822. decimal : str, default '.'
  823. Character to recognize as decimal point (e.g. use ',' for European
  824. data).
  825. converters : dict, default None
  826. Dict of functions for converting values in certain columns. Keys can
  827. either be integers or column labels, values are functions that take one
  828. input argument, the cell (not column) content, and return the
  829. transformed content.
  830. na_values : iterable, default None
  831. Custom NA values.
  832. keep_default_na : bool, default True
  833. If na_values are specified and keep_default_na is False the default NaN
  834. values are overridden, otherwise they're appended to.
  835. displayed_only : bool, default True
  836. Whether elements with "display: none" should be parsed.
  837. Returns
  838. -------
  839. dfs
  840. A list of DataFrames.
  841. See Also
  842. --------
  843. read_csv : Read a comma-separated values (csv) file into DataFrame.
  844. Notes
  845. -----
  846. Before using this function you should read the :ref:`gotchas about the
  847. HTML parsing libraries <io.html.gotchas>`.
  848. Expect to do some cleanup after you call this function. For example, you
  849. might need to manually assign column names if the column names are
  850. converted to NaN when you pass the `header=0` argument. We try to assume as
  851. little as possible about the structure of the table and push the
  852. idiosyncrasies of the HTML contained in the table to the user.
  853. This function searches for ``<table>`` elements and only for ``<tr>``
  854. and ``<th>`` rows and ``<td>`` elements within each ``<tr>`` or ``<th>``
  855. element in the table. ``<td>`` stands for "table data". This function
  856. attempts to properly handle ``colspan`` and ``rowspan`` attributes.
  857. If the function has a ``<thead>`` argument, it is used to construct
  858. the header, otherwise the function attempts to find the header within
  859. the body (by putting rows with only ``<th>`` elements into the header).
  860. Similar to :func:`~read_csv` the `header` argument is applied
  861. **after** `skiprows` is applied.
  862. This function will *always* return a list of :class:`DataFrame` *or*
  863. it will fail, e.g., it will *not* return an empty list.
  864. Examples
  865. --------
  866. See the :ref:`read_html documentation in the IO section of the docs
  867. <io.read_html>` for some examples of reading in HTML tables.
  868. """
  869. _importers()
  870. # Type check here. We don't want to parse only to fail because of an
  871. # invalid value of an integer skiprows.
  872. if isinstance(skiprows, numbers.Integral) and skiprows < 0:
  873. raise ValueError(
  874. "cannot skip rows starting from the end of the "
  875. "data (you passed a negative value)"
  876. )
  877. validate_header_arg(header)
  878. io = stringify_path(io)
  879. return _parse(
  880. flavor=flavor,
  881. io=io,
  882. match=match,
  883. header=header,
  884. index_col=index_col,
  885. skiprows=skiprows,
  886. parse_dates=parse_dates,
  887. thousands=thousands,
  888. attrs=attrs,
  889. encoding=encoding,
  890. decimal=decimal,
  891. converters=converters,
  892. na_values=na_values,
  893. keep_default_na=keep_default_na,
  894. displayed_only=displayed_only,
  895. )