如何使用python获取某些内容?<table> </table>

时间:2012-10-02 12:31:37

标签: python regex

我有一些<tr>,像这样:

<tr align=center><td>10876151</td><td><a href=userstatus?user_id=yangfanhit>yangfanhit</a></td><td><a href=problem?id=3155>3155</a></td><td><font color=blue>Accepted</font></td><td>344K</td><td>219MS</td><td>C++</td><td>3940B</td><td>2012-10-02 16:42:45</td></tr>
<tr align=center><td>10876150</td><td><a href=userstatus?user_id=BandBandRock>BandBandRock</a></td><td><a href=problem?id=2503>2503</a></td><td><font color=blue>Accepted</font></td><td>16348K</td><td>2750MS</td><td>G++</td><td>840B</td><td>2012-10-02 16:42:25</td></tr>

我想获取没有html标签的内容,例如:

yangfanhit
3155
Accepted
344K
219MS
C++
3940B
2012-10-02 16:42:45

现在我正在使用以下代码来处理它:

response = urllib2.urlopen('http://poj.org/status', timeout=10)
html = response.read()
response.close()

pattern = re.compile(r'<tr align.*</tr>')
match = pattern.findall(html)
pat = re.compile(r'<td>.*?</td>')
p = re.compile(r'<[/]?.*?>')
for item in match:
    for i in pat.findall(item):
        print p.sub(r'', i)
    print '================================================='

我是regex的新手,也是python的新手。那么你能建议一些更好的方法来处理它吗?

4 个答案:

答案 0 :(得分:1)

您可以使用BeautifulSoup来解析html。以csv格式编写表的内容:

#!/usr/bin/env python
import csv
import sys
import urllib2
from bs4 import BeautifulSoup # $ pip install beautifulsoup4

soup = BeautifulSoup(urllib2.urlopen('http://poj.org/status'))

writer = csv.writer(sys.stdout)
for tr in soup.find('table', 'a')('tr'):
    writer.writerow([td.get_text() for td in tr('td')])

输出

Run ID,User,Problem,Result,Memory,Time,Language,Code Length,Submit Time
10876151,yangfanhit,3155,Accepted,344K,219MS,C++,3940B,2012-10-02 16:42:45
10876150,BandBandRock,2503,Accepted,16348K,2750MS,G++,840B,2012-10-02 16:42:25

答案 1 :(得分:1)

另请查看PyQuery。如果你熟悉jQuery,很容易拾取。这是一个将表头和数据作为字典列表返回的示例。

import itertools
from pyquery import PyQuery as pq

# parse html
html = pq(url="http://poj.org/status")

# extract header values from table
header = [header.text for header in html(".a").find(".in").find("td")]

# extract data values from table rows in nested list
detail = [[td.text for td in tr] for tr in html(".a").children().not_(".in")]

# merge header and detail to create list of dictionaries
result = [dict(itertools.izip(header, values)) for values in detail]

答案 2 :(得分:0)

您真的不需要直接使用regex来解析html,see answer here

或者查看有关HTML处理的Dive into Python Chapter 8

答案 3 :(得分:0)

为什么当你已经拥有可以轻松完成工作的HTML / XML解析器时,你正在做这些事情

使用BeautifulSoup。考虑到上面问题中提到的你想要的东西,它可以用2-3行代码完成。

示例:

>>> from bs4 import BeautifulSoup as bs
>>> html = """
<tr align=center><td>10876151</td><td><a href=userstatus?user_id=yangfanhit>yangfanhit</a></td><td><a href=problem?id=3155>3155</a></td><td><font color=blue>Accepted</font></td><td>344K</td><td>219MS</td><td>C++</td><td>3940B</td><td>2012-10-02 16:42:45</td></tr>
<tr align=center><td>10876150</td><td><a href=userstatus?user_id=BandBandRock>BandBandRock</a></td><td><a href=problem?id=2503>2503</a></td><td><font color=blue>Accepted</font></td><td>16348K</td><td>2750MS</td><td>G++</td><td>840B</td><td>2012-10-02 16:42:25</td></tr>
"""

>>>soup = bs(html)
>>>soup.td
>>><td>10876151</td>