html.py 39 KB

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