html_zhch.py 41 KB

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