Python实现简单HTML表格解析的方法
本文实例讲述了Python实现简单HTML表格解析的方法。分享给大家供大家参考。具体分析如下:
这里依赖libxml2dom,确保首先安装!导入到你的脚步并调用parse_tables() 函数。
1. source = a string containing the source code you can pass in just the table or the entire page code
2. headers = a list of ints OR a list of strings
If the headers are ints this is for tables with no header, just list the 0 based index of the rows in which you want to extract data.
If the headers are strings this is for tables with header columns (with the tags) it will pull the information from the specified columns
3. The 0 based index of the table in the source code. If there are multiple tables and the table you want to parse is the third table in the code then pass in the number 2 here
It will return a list of lists. each inner list will contain the parsed information.
具体代码如下:
#The goal of table parser is to get specific information from specific #columns in a table. #Input: source code from a typical website #Arguments: a list of headers the user wants to return #Output: A list of lists of the data in each row import libxml2dom def parse_tables(source, headers, table_index): """parse_tables(string source, list headers, table_index) headers may be a list of strings if the table has headers defined or headers may be a list of ints if no headers defined this will get data from the rows index. This method returns a list of lists """ #Determine if the headers list is strings or ints and make sure they #are all the same type j = 0 print 'Printing headers: ',headers #route to the correct function #if the header type is int if type(headers[0]) == type(1): #run no_header function return no_header(source, headers, table_index) #if the header type is string elif type(headers[0]) == type('a'): #run the header_given function return header_given(source, headers, table_index) else: #return none if the headers aren't correct return None #This function takes in the source code of the whole page a string list of #headers and the index number of the table on the page. It returns a list of #lists with the scraped information def header_given(source, headers, table_index): #initiate a list to hole the return list return_list = [] #initiate a list to hold the index numbers of the data in the rows header_index = [] #get a document object out of the source code doc = libxml2dom.parseString(source,html=1) #get the tables from the document tables = doc.getElementsByTagName('table') try: #try to get focue on the desired table main_table = tables[table_index] except: #if the table doesn't exits then return an error return ['The table index was not found'] #get a list of headers in the table table_headers = main_table.getElementsByTagName('th') #need a sentry value for the header loop loop_sentry = 0 #loop through each header looking for matches for header in table_headers: #if the header is in the desired headers list if header.textContent in headers: #add it to the header_index header_index.append(loop_sentry) #add one to the loop_sentry loop_sentry+=1 #get the rows from the table rows = main_table.getElementsByTagName('tr') #sentry value detecting if the first row is being viewed row_sentry = 0 #loop through the rows in the table, skipping the first row for row in rows: #if row_sentry is 0 this is our first row if row_sentry == 0: #make the row_sentry not 0 row_sentry = 1337 continue #get all cells from the current row cells = row.getElementsByTagName('td') #initiate a list to append into the return_list cell_list = [] #iterate through all of the header index's for i in header_index: #append the cells text content to the cell_list cell_list.append(cells[i].textContent) #append the cell_list to the return_list return_list.append(cell_list) #return the return_list return return_list #This function takes in the source code of the whole page an int list of #headers indicating the index number of the needed item and the index number #of the table on the page. It returns a list of lists with the scraped info def no_header(source, headers, table_index): #initiate a list to hold the return list return_list = [] #get a document object out of the source code doc = libxml2dom.parseString(source, html=1) #get the tables from document tables = doc.getElementsByTagName('table') try: #Try to get focus on the desired table main_table = tables[table_index] except: #if the table doesn't exits then return an error return ['The table index was not found'] #get all of the rows out of the main_table rows = main_table.getElementsByTagName('tr') #loop through each row for row in rows: #get all cells from the current row cells = row.getElementsByTagName('td') #initiate a list to append into the return_list cell_list = [] #loop through the list of desired headers for i in headers: try: #try to add text from the cell into the cell_list cell_list.append(cells[i].textContent) except: #if there is an error usually an index error just continue continue #append the data scraped into the return_list return_list.append(cell_list) #return the return list return return_list
希望本文所述对大家的Python程序设计有所帮助。

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

羽化控制的關鍵在於理解其漸變本質。 PS本身不提供直接控制漸變曲線的選項,但你可以通過多次羽化、配合蒙版、精細選區,靈活調整半徑和漸變柔和度,實現自然過渡效果。

MySQL 有免費的社區版和收費的企業版。社區版可免費使用和修改,但支持有限,適合穩定性要求不高、技術能力強的應用。企業版提供全面商業支持,適合需要穩定可靠、高性能數據庫且願意為支持買單的應用。選擇版本時考慮的因素包括應用關鍵性、預算和技術技能。沒有完美的選項,只有最合適的方案,需根據具體情況謹慎選擇。

文章介紹了MySQL數據庫的上手操作。首先,需安裝MySQL客戶端,如MySQLWorkbench或命令行客戶端。 1.使用mysql-uroot-p命令連接服務器,並使用root賬戶密碼登錄;2.使用CREATEDATABASE創建數據庫,USE選擇數據庫;3.使用CREATETABLE創建表,定義字段及數據類型;4.使用INSERTINTO插入數據,SELECT查詢數據,UPDATE更新數據,DELETE刪除數據。熟練掌握這些步驟,並學習處理常見問題和優化數據庫性能,才能高效使用MySQL。

PS羽化是一種圖像邊緣模糊效果,通過在邊緣區域對像素加權平均實現。設置羽化半徑可以控制模糊程度,數值越大越模糊。靈活調整半徑可根據圖像和需求優化效果,如處理人物照片時使用較小半徑保持細節,處理藝術作品時使用較大半徑營造朦朧感。但需注意,半徑過大易丟失邊緣細節,過小則效果不明顯。羽化效果受圖像分辨率影響,且需要根據圖像理解和效果把握進行調整。

MySQL安裝失敗的原因主要有:1.權限問題,需以管理員身份運行或使用sudo命令;2.依賴項缺失,需安裝相關開發包;3.端口衝突,需關閉佔用3306端口的程序或修改配置文件;4.安裝包損壞,需重新下載並驗證完整性;5.環境變量配置錯誤,需根據操作系統正確配置環境變量。解決這些問題,仔細檢查每個步驟,就能順利安裝MySQL。

MySQL性能優化需從安裝配置、索引及查詢優化、監控與調優三個方面入手。 1.安裝後需根據服務器配置調整my.cnf文件,例如innodb_buffer_pool_size參數,並關閉query_cache_size;2.創建合適的索引,避免索引過多,並優化查詢語句,例如使用EXPLAIN命令分析執行計劃;3.利用MySQL自帶監控工具(SHOWPROCESSLIST,SHOWSTATUS)監控數據庫運行狀況,定期備份和整理數據庫。通過這些步驟,持續優化,才能提升MySQL數據庫性能。

MySQL下載文件損壞,咋整?哎,下載個MySQL都能遇到文件損壞,這年頭真是不容易啊!這篇文章就來聊聊怎麼解決這個問題,讓大家少走彎路。讀完之後,你不僅能修復損壞的MySQL安裝包,還能對下載和安裝過程有更深入的理解,避免以後再踩坑。先說說為啥下載文件會損壞這原因可多了去了,網絡問題是罪魁禍首,下載過程中斷、網絡不穩定都可能導致文件損壞。還有就是下載源本身的問題,服務器文件本身就壞了,你下載下來當然也是壞的。另外,一些殺毒軟件過度“熱情”的掃描也可能造成文件損壞。診斷問題:確定文件是否真的損壞

MySQL數據庫性能優化指南在資源密集型應用中,MySQL數據庫扮演著至關重要的角色,負責管理海量事務。然而,隨著應用規模的擴大,數據庫性能瓶頸往往成為製約因素。本文將探討一系列行之有效的MySQL性能優化策略,確保您的應用在高負載下依然保持高效響應。我們將結合實際案例,深入講解索引、查詢優化、數據庫設計以及緩存等關鍵技術。 1.數據庫架構設計優化合理的數據庫架構是MySQL性能優化的基石。以下是一些核心原則:選擇合適的數據類型選擇最小的、符合需求的數據類型,既能節省存儲空間,又能提升數據處理速度
