Pandas.read_html() 獲取靜態(tài)網(wǎng)頁(yè)表格數(shù)據(jù)

環(huán)境:Win10 + Cmder + Python3.6.5

需求

? 獲取 http://www.air-level.com/air/xian/ 的空氣質(zhì)量指數(shù)表格數(shù)據(jù)。騷年,是不是蠢蠢欲動(dòng)要爬蟲三步走了?

代碼

我說(shuō)三行代碼就可以輕松搞定, 你信嗎?(正經(jīng)臉):

import pandas as pd
df = pd.read_html("http://www.air-level.com/air/xian/", encoding='utf-8', header=0)[0]
df.to_excel('xian_tianqi.xlsx', index=False)

? 然后先來(lái)看網(wǎng)頁(yè)數(shù)據(jù):

? 再來(lái)看Excel中的數(shù)據(jù):


? 是不是被秀到啦?講真,我也被秀到一臉...

解釋

? read_html()部分源碼如下:

# 已省略部分代碼,詳細(xì)查看可在命令行執(zhí)行:print(pd.read_html.__doc__)
def read_html(io, match='.+', flavor=None, header=None, index_col=None,
              skiprows=None, attrs=None, parse_dates=False,
              tupleize_cols=None, thousands=',', encoding=None,
              decimal='.', converters=None, na_values=None,
              keep_default_na=True, displayed_only=True):
    r"""Read HTML tables into a ``list`` of ``DataFrame`` objects.

  Parameters
    ----------
    io : str or file-like
        A URL, a file-like object, or a raw string containing HTML. Note that
        lxml only accepts the http, ftp and file url protocols. If you have a
        URL that starts with ``'https'`` you might try removing the ``'s'``.

    flavor : str or None, container of strings
        The parsing engine to use. 'bs4' and 'html5lib' are synonymous with
        each other, they are both there for backwards compatibility. The
        default of ``None`` tries to use ``lxml`` to parse and if that fails it
        falls back on ``bs4`` + ``html5lib``.

     header : int or list-like or None, optional
        The row (or list of rows for a :class:`~pandas.MultiIndex`) to use to
        make the columns headers.
......

? 可以看到,read_html() 方法的 io 參數(shù)默認(rèn)了多種形式,URL 便是其中一種。然后函數(shù)默認(rèn)調(diào)用 lxml 解析 table 標(biāo)簽里的每個(gè) td 的數(shù)據(jù),最后生成一個(gè)包含 Dataframe 對(duì)象的列表。通過(guò)索引獲取到 DataFrame 對(duì)象即可。

最后

? read_html() 僅支持靜態(tài)網(wǎng)頁(yè)解析。你可以通過(guò)其他方法獲取動(dòng)態(tài)頁(yè)面加載后response.text 傳入 read_html() 再獲取表格數(shù)據(jù)。

參考https://mp.weixin.qq.com/s/CuhC7rCD6LPXLO88JVEuJg

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容