regex / beautifulsoup如何从html表中提取列的所有值?

时间:2016-03-07 08:44:19

标签: python html regex beautifulsoup

从这段代码:

<tr><td>PC1</td><td>zz:zz:zz:zz:zz:ce</td><td>10.0.0.244</td><td>23 hours, 55 minutes, 25 seconds</td></tr>
<tr><td>PC2</td><td>zz:zz:zz:zz:zz:cf</td><td>10.0.0.245</td><td>23 hours, 23 minutes, 27 seconds</td></tr>

我想获得一系列MAC地址和另一组ip

我认为像macs的正则​​表达式一样:<\/td><td>(.*?){17}<\/td> 但它也符合正常运行时间。

有什么建议吗?

谢谢!

2 个答案:

答案 0 :(得分:3)

从您提供的html开始,您可以执行以下操作:

from bs4 import BeautifulSoup

html = """<tr><td>PC1</td><td>zz:zz:zz:zz:zz:ce</td><td>10.0.0.244</td><td>23 hours, 55 minutes, 25 seconds</td></tr>
<tr><td>PC2</td><td>zz:zz:zz:zz:zz:cf</td><td>10.0.0.245</td><td>23 hours, 23 minutes, 27 seconds</td></tr>"""

soup = BeautifulSoup(html)
mac_ips = []

for tr in soup.find_all('tr'):
    cols = [td.text for td in tr.find_all('td')]
    mac_ips.append((cols[1], cols[2]))

for mac, ip in mac_ips:
    print '{}  {}'.format(mac, ip)

给你:

zz:zz:zz:zz:zz:ce  10.0.0.244
zz:zz:zz:zz:zz:cf  10.0.0.245

mac_ips会将每一行保持为匹配对:

[(u'zz:zz:zz:zz:zz:ce', u'10.0.0.244'), (u'zz:zz:zz:zz:zz:cf', u'10.0.0.245')]

如果要分隔列表,则可以执行以下操作:

from bs4 import BeautifulSoup

html = """<tr><td>PC1</td><td>zz:zz:zz:zz:zz:ce</td><td>10.0.0.244</td><td>23 hours, 55 minutes, 25 seconds</td></tr>
<tr><td>PC2</td><td>zz:zz:zz:zz:zz:cf</td><td>10.0.0.245</td><td>23 hours, 23 minutes, 27 seconds</td></tr>"""

soup = BeautifulSoup(html)
mac = []
ip = []

for tr in soup.find_all('tr'):
    cols = [td.text for td in tr.find_all('td')]
    mac.append(cols[1])
    ip.append(cols[2])

print mac
print ip

给你:

[u'zz:zz:zz:zz:zz:ce', u'zz:zz:zz:zz:zz:cf']
[u'10.0.0.244', u'10.0.0.245']

注意:如果您要解析更多html,那么您可能还需要先找到封闭的<table>

答案 1 :(得分:-2)

try:
    table = soup.find('table')
except AttributeError as e:
    print 'No tables found, exiting'
    return 1

# Get rows
try:
    rows = table.find_all('tr')
except AttributeError as e:
    print 'No table rows found, exiting'
    return 1