Python库pandas之七
- 输入/输出
- read_html
- pandas.read_html(io, *, match='.+', flavor=None, header=None, index_col=None, skiprows=None, attrs=None, parse_dates=False, thousands=', ', encoding=None, decimal='.', converters=None, na_values=None, keep_default_na=True, displayed_only=True, extract_links=None, dtype_backend=<no_default>, storage_options=None)
- 应用实列
输入/输出
read_html
pandas.read_html(io, *, match=‘.+’, flavor=None, header=None, index_col=None, skiprows=None, attrs=None, parse_dates=False, thousands=‘, ‘, encoding=None, decimal=’.’, converters=None, na_values=None, keep_default_na=True, displayed_only=True, extract_links=None, dtype_backend=<no_default>, storage_options=None)
将 HTML表读入DataFrame对象列表中。
参数说明
-
io,该参数类型是字符串, path对象, 或类似文件的对象
该参数是字符串、路径对象(实现 os.PathLike[str]),或实现字符串 read() 函数的类文件对象。该字符串可以表示 URL 或 HTML 本身。lxml 仅接受 http、ftp 和文件 url 协议。 -
match,该参数类型是字符串,或编译后的正则表达式, 是可选的
将返回包含与此正则表达式或字符串匹配的文本的表集。除非 HTML 非常简单,否则您可能需要在此处传递一个非空字符串。默认为“.+”(匹配任何非空字符串)。默认值将返回页面上包含的所有表格。该值将转换为正则表达式,以便 Beautiful Soup 和 lxml 之间具有一致的行为。 -
flavor,该参数类型是{“lxml”, “html5lib”, “bs4”} 或i类似list, 是可选的
要使用的解析引擎(或解析引擎列表)。 “bs4”和“html5lib”是同义词,它们都是为了向后兼容。默认值None,尝试使用 lxml 进行解析,如果失败,则返回到 bs4 + html5lib。 -
header,该参数类型是int,或类似list, 是可选的
该参数是用于制作列标题的行,该参数也可以用于MultiIndex的行列表。 -
index_col,该参数类型是int,或类似list, 是可选的
用于创建索引的列,或列list。 -
skiprows,该参数类型是int, 类似list,或slice, 是可选的
解析列整数后要跳过的行数。 0 为基础。如果给出了整数序列或切片,则将跳过该序列索引的行。单个元素序列表示“跳过第 n 行”,而整数表示“跳过 n 行”。 -
attrs,该参数类型是dict, 是可选的
attrs是一个属性字典,可以用于标识HTML中的表。在传递给 lxml 或 Beautiful Soup 之前,不会检查它们的有效性。但是,这些属性必须是有效的,HTML表属性才能正常工作。例如,attrs = {'id': 'table'}
是一个有效的属性字典,因为“id”HTML 标签属性对于本文档中的任何HTML标签来说,都是有效的 HTML 属性。
attrs = {'asdf': 'table'}
不是有效的属性字典,因为“asdf”不是有效的HTML属性,即使它是有效的XML属性。可以在此处找到有效的 HTML 4.01 表属性。可以在此处找到 HTML 5 规范的工作草案。它包含有关现代网络表属性的最新信息。
-
parse_dates,该参数类型是bool, 是可选的
类似read_csv() -
thousands,该参数类型是字符串, 是可选的
用于解析数值千位的分隔符。默认为“,”。 -
encoding,该参数类型是字符串, 是可选的
用于解码网页的编码。默认为None。None保留以前的编码行为,这取决于底层解析器库(例如,解析器库将尝试使用文档提供的编码)。 -
decimal,该参数类型是字符串, default ‘.’
识别为小数点的字符。 -
converters,该参数类型是dict, 默认值为None
该参数是一个函数字典,用于转换某些列中的值。该字典的键可以是整数或列标签,值是采用一个输入参数、单元格(而不是列)内容并返回转换后的内容的函数。 -
na_values,该参数类型是iterable, 默认值为None
自定义 NA 值。 -
keep_default_na,该参数类型是bool, 默认值为True
如果指定了 na_values,并且 keep_default_na 为 False,则默认 NaN 值将被覆盖,否则将附加它们。 -
displayed_only,该参数类型是bool, 默认值为True
是否应该解析带有“display: none”的元素。 -
extract_links,该参数类型是{None, “all”, “header”, “body”, “footer”}
带有 标记的指定部分中的表元素将提取其 href。 -
dtype_backend,该参数类型是{‘numpy_nullable’, ‘pyarrow’}, 默认值为‘numpy_nullable’
应用于生成的DataFrame的后端数据类型。行为如下:
“numpy_nullable”:返回支持 nullable-dtype 的 DataFrame(默认)。
“pyarrow”:返回 pyarrow 支持的可为空的 ArrowDtype DataFrame。 -
storage_options,该参数类型是dict, 是可选的
对于特定存储连接有意义的额外选项,例如主机、端口、用户名、密码等。对于 HTTP(S) URL,键值对将作为标头选项转发到 urllib.request.Request。对于其他 URL(例如以“s3://”和“gcs://”开头),键值对将转发到 fsspec.open。
应用实列
下列代码从https://en.wikipedia.org/wiki/Minnesota站点读HTML,得到表格数据
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from unicodedata import normalizedef f_read_html(http_str, match_str):table_MN = pd.read_html(http_str, match=match_str)df = table_MN[0]print(df)cols_l = list(df.index)for col in cols_l:print("----"*9)s1 = df.iloc[col]index = list(s1.index)print(index)vals = list(s1.array)print(vals)col_labels = list(df.columns)print()for col in col_labels:print()print("------------{0}-----------".format(col))l1 = list(df[col])print("{0}".format(l1))if __name__=="__main__":http_str = 'https://en.wikipedia.org/wiki/Minnesota'match_str = 'Average daily maximum and minimum temperatures for selected cities in Minnesota' f_read_html(http_str, match_str)match_str = 'Largest cities or towns in Minnesota'
C:\>python pandas_io_5.pyLocation July (°F) July (°C) January (°F) January (°C)
0 Minneapolis 83/64 28/18 23/7 −4/−13
1 Saint Paul 83/63 28/17 23/6 −5/−14
2 Rochester 82/63 28/17 23/3 −5/−16
3 Duluth 76/55 24/13 19/1 −7/−17
4 St. Cloud 81/58 27/14 18/−1 −7/−18
5 Mankato 86/62 30/16 23/3 −5/−16
6 International Falls 77/52 25/11 15/−6 −9/−21
------------------------------------
['Location', 'July (°F)', 'July (°C)', 'January (°F)', 'January (°C)']
['Minneapolis', '83/64', '28/18', '23/7', '−4/−13']
------------------------------------
['Location', 'July (°F)', 'July (°C)', 'January (°F)', 'January (°C)']
['Saint Paul', '83/63', '28/17', '23/6', '−5/−14']
------------------------------------
['Location', 'July (°F)', 'July (°C)', 'January (°F)', 'January (°C)']
['Rochester', '82/63', '28/17', '23/3', '−5/−16']
------------------------------------
['Location', 'July (°F)', 'July (°C)', 'January (°F)', 'January (°C)']
['Duluth', '76/55', '24/13', '19/1', '−7/−17']
------------------------------------
['Location', 'July (°F)', 'July (°C)', 'January (°F)', 'January (°C)']
['St. Cloud', '81/58', '27/14', '18/−1', '−7/−18']
------------------------------------
['Location', 'July (°F)', 'July (°C)', 'January (°F)', 'January (°C)']
['Mankato', '86/62', '30/16', '23/3', '−5/−16']
------------------------------------
['Location', 'July (°F)', 'July (°C)', 'January (°F)', 'January (°C)']
['International Falls', '77/52', '25/11', '15/−6', '−9/−21']------------Location-----------
['Minneapolis', 'Saint Paul', 'Rochester', 'Duluth', 'St. Cloud', 'Mankato', 'International Falls']------------July (°F)-----------
['83/64', '83/63', '82/63', '76/55', '81/58', '86/62', '77/52']------------July (°C)-----------
['28/18', '28/17', '28/17', '24/13', '27/14', '30/16', '25/11']------------January (°F)-----------
['23/7', '23/6', '23/3', '19/1', '18/−1', '23/3', '15/−6']------------January (°C)-----------
['−4/−13', '−5/−14', '−5/−16', '−7/−17', '−7/−18', '−5/−16', '−9/−21']