html.py 34 KB

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