python beautifulsoup4 table tr_python BeautifulSoup解析表
生活随笔
收集整理的這篇文章主要介紹了
python beautifulsoup4 table tr_python BeautifulSoup解析表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
牧羊人nacy
這是通用的工作示例
| (表數據)標記。它返回帶有內部列的行的列表。 | 第一行僅接受一個(表頭/數據)。def tableDataText(table):? ? ? ?? ? rows = []? ? trs = table.find_all('tr')? ? headerow = [td.get_text(strip=True) for td in trs[0].find_all('th')] # header row? ? if headerow: # if there is a header row include first? ? ? ? rows.append(headerow)? ? ? ? trs = trs[1:]? ? for tr in trs: # for every table row? ? ? ? rows.append([td.get_text(strip=True) for td in tr.find_all('td')]) # data row? ? return rows使用它,我們得到(前兩行)。list_table = tableDataText(htmltable)list_table[:2][['Rank',? 'Name',? "GDP (IMF '19)",? "GDP (UN '16)",? 'GDP Per Capita',? '2019 Population'],?['1',? 'United States',? '21.41 trillion',? '18.62 trillion',? '$65,064',? '329,064,917']]可以輕松地將其轉換pandas.DataFrame為更高級的工具。import pandas as pddftable = pd.DataFrame(list_table[1:], columns=list_table[0])dftable.head(4)
總結
以上是生活随笔為你收集整理的python beautifulsoup4 table tr_python BeautifulSoup解析表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql mgr简介_MySQL Gr
- 下一篇: java反射有什么用_java反射的作用