Convert HTML Table to an Array in Python
来源:互联网 发布:贵金属软件下载 编辑:程序博客网 时间:2024/06/06 07:28
It's been quite a long time since my last update.
Currently, I'm working as an intern in one of the GPS. Well, life now is quite different from what was in school.
My first task is to extract some information from hundreds of HTML(4.0) files and fill the useful ones to Excel files.I heard about a famousGoogle's engineering decision:"Python where we can, C++ where we must".So I decided to learn python and adopt it in this task.
Python 2.7.3 + beautifulSoup 4.1.3
An important procedure is to convert HTML tables to arrays like those in Matlab and OpenCV.
Here's what we've already got.
http://stackoverflow.com/questions/2870667/how-to-convert-an-html-table-to-an-array-in-python
def makelist(table): result = [] allrows = table.findAll('tr') for row in allrows: result.append([]) allcols = row.findAll('td') for col in allcols: thestrings = [unicode(s) for s in col.findAll(text=True)] thetext = ''.join(thestrings) result[-1].append(thetext) return result
For example
<TABLE ID = "3" BORDER=1 WIDTH="100%" COLS=3><CAPTION ALIGN="LEFT">2.2 Prepare</CAPTION><COLGROUP ALIGN="CENTER" SPAN=3><TBODY><TR><TD COLSPAN = "3"><B>Plane Table</B></TD></TR><TR><TH COLSPAN = "3">Prefiltration</TH></TR><TR><TH>blabla<BR>[mm]</TH><TH>blabla<BR>[mm]</TH><TH>blabla<BR>[mm]</TH></TR><TR><TD>2.50</TD><TD>1.00</TD><TD>3.50</TD></TR></TABLE>Each col in result would be 1, 0, 0, 3 using the above python code.However, one might wanna get result like 3, 0, 0, 3(then you can get 0, 3, 3, 0 just replace 'td' with 'th').
The key point to solve this problem is to get the attribute 'colspan'.
Here is the solution:
def makelist(table): result = [] allrows = table.find_all('tr') for row in allrows: result.append([]) allcols = row.find_all('td') for col in allcols: thestrings = [unicode(s) for s in col.find_all(text=True)] thetext = ''.join(thestrings) result[-1].append(thetext) if col.has_attr('colspan'):#if colspan != 0, filling blanks colspan = col.attrs['colspan']# or colspan = col['colspan'] for i in range(int(colspan)-1): result[-1].append(unicode('')) return result,len(allrows),int(table['cols'])
If labels <td> and <th> are mixed in the same row, you can simply change
allcols = row.find_all('td')to:
allcols = row.find_all(["td","th"])then a table actually becomes 'one' table.
- Convert HTML Table to an Array in Python
- javascript数组转换(convert to an Array)
- How to define an array in c#.
- How to initialize an array in C
- Convert Html to pdf in iOS
- How to convert char array to String in Java
- href of python convert HTML to XML
- Converting a array to an array of ranges in ruby
- Convert infix to postfix in Python
- Convert byte array to char* in a clr wrapper
- 【Leetcode】Convert Sorted Array to Binary Search Tree in JAVA
- How to format strings in an array in ruby?
- Convert array to Set
- Leetcode -- Python -- Convert Sorted Array to Binary Search Tree
- 【LeetCode with Python】 Convert Sorted Array to Binary Search Tree
- 108. Convert Sorted Array to Binary Search Tree Leetcode Python
- cracking the code interview convert array to BST Python
- [leetcode: Python] 108. Convert Sorted Array to Binary Search Tree
- MyEclipse8.5自动生成注册码
- ubuntu 下安装jdk
- 取球游戏 今盒子里有n个小球,A、B两人轮流从盒中取球,每个人都可以看到另一个人取了多少个, 也可以看到盒中还剩下多少个,并且两人都很聪明,不会做出错误的判断。
- linux常用命令
- 软件测试面试题及解析(五)
- Convert HTML Table to an Array in Python
- copy, retain, assign , readonly , readwrite,strong,weak,nonatomic整理
- 手动搭建SSH框架
- bss段,代码段及数据段的区别
- java函数传值还是传引用的问题
- zbar中文乱码的解决
- MFC问题集之单选按钮进行分组
- metasploit扫描ssh
- iOS ARC