最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實(shí)現(xiàn)簡(jiǎn)單HTML表格解析的方法

 更新時(shí)間:2015年06月15日 14:48:50   作者:小卒過(guò)河  
這篇文章主要介紹了Python實(shí)現(xiàn)簡(jiǎn)單HTML表格解析的方法,涉及Python基于libxml2dom模塊操作html頁(yè)面元素的技巧,需要的朋友可以參考下

本文實(shí)例講述了Python實(shí)現(xiàn)簡(jiǎn)單HTML表格解析的方法。分享給大家供大家參考。具體分析如下:

這里依賴libxml2dom,確保首先安裝!導(dǎo)入到你的腳步并調(diào)用parse_tables() 函數(shù)。

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

希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • 解決python Markdown模塊亂碼的問(wèn)題

    解決python Markdown模塊亂碼的問(wèn)題

    今天小編就為大家分享一篇解決python Markdown模塊亂碼的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-02-02
  • Tensorflow 如何從checkpoint文件中加載變量名和變量值

    Tensorflow 如何從checkpoint文件中加載變量名和變量值

    這篇文章主要介紹了Tensorflow 如何從checkpoint文件中加載變量名和變量值的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • Python實(shí)現(xiàn)的簡(jiǎn)單文件傳輸服務(wù)器和客戶端

    Python實(shí)現(xiàn)的簡(jiǎn)單文件傳輸服務(wù)器和客戶端

    這篇文章主要介紹了Python實(shí)現(xiàn)的簡(jiǎn)單文件傳輸服務(wù)器和客戶端,本文直接給出Server和Client端的實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2015-04-04
  • 詳解如何利用Python拍攝延時(shí)攝影

    詳解如何利用Python拍攝延時(shí)攝影

    隨著游戲引擎技術(shù)的快速發(fā)展,游戲畫(huà)面越來(lái)越精美,很多玩家希望拍攝這些精美游戲中的畫(huà)面。本文將講解如何利用Python實(shí)現(xiàn)延時(shí)攝影的拍攝,需要的可以參考一下
    2022-03-03
  • 如何用Python Beautiful?Soup解析HTML內(nèi)容

    如何用Python Beautiful?Soup解析HTML內(nèi)容

    Beautiful Soup是一種Python的解析庫(kù),主要用于解析和處理HTML/XML內(nèi)容,詳細(xì)介紹Beautiful Soup的使用方式和應(yīng)用場(chǎng)景,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • python判斷集合的超集方法及實(shí)例

    python判斷集合的超集方法及實(shí)例

    在本篇內(nèi)容里小編給大家分享的是一篇關(guān)于python判斷集合的超集方法及實(shí)例內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。
    2021-05-05
  • 8個(gè)Python必備的PyCharm插件(附下載地址)

    8個(gè)Python必備的PyCharm插件(附下載地址)

    Python是一種廣泛使用的編程語(yǔ)言,PyCharm是最受歡迎的Python IDE之一,本文就來(lái)介紹一下8個(gè)Python必備的PyCharm插件,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • Python Tkinter之事件處理詳解

    Python Tkinter之事件處理詳解

    事件處理,是 GUI 程序中不可或缺的重要組成部分,相比來(lái)說(shuō),控件只是組成一臺(tái)機(jī)器的零部件。本文我們將對(duì) Tkinter 中的事件處理機(jī)制做詳細(xì)的介紹,需要的可以參考一下
    2022-01-01
  • python numpy中對(duì)ndarry按照index增刪改查

    python numpy中對(duì)ndarry按照index增刪改查

    這篇文章主要介紹了python numpy中對(duì)ndarry按照index增刪改查,在numpy中的ndarry是一個(gè)數(shù)組,因此index就是位置下標(biāo),注意下標(biāo)是從0開(kāi)始,接下來(lái)一起進(jìn)入下面文章了解詳細(xì)內(nèi)容吧
    2022-02-02
  • Django中l(wèi)ogin_required裝飾器的深入介紹

    Django中l(wèi)ogin_required裝飾器的深入介紹

    這篇文章主要給大家介紹了關(guān)于Django中l(wèi)ogin_required裝飾器的使用方法,并給大家進(jìn)行了實(shí)例借鑒,利用@login_required實(shí)現(xiàn)Django用戶登陸訪問(wèn)限制,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-11-11

最新評(píng)論

尼勒克县| 铁岭市| 宝丰县| 临沧市| 赤峰市| 泰和县| 福清市| 长垣县| 恩平市| 烟台市| 邹城市| 滨州市| 二连浩特市| 石嘴山市| 岫岩| 宣汉县| 万安县| 彰化市| 达日县| 遂宁市| 许昌市| 股票| 聊城市| 陵水| 万年县| 江孜县| 宁津县| 海晏县| 四会市| 德惠市| 濮阳县| 台东县| 沂水县| 遵义市| 镇沅| 鹰潭市| 平昌县| 灌南县| 中方县| 乌苏市| 垦利县|