本文實例講述了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.
具體代碼如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118#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