regex / beautifulsoup how to extract all values ​​from the html table column?

advertisements

From this code:

<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>

I'd like to get an array of MAC addresses and another array of ip's

I thought something like that regex for the macs: <\/td><td>(.*?){17}<\/td> but it match the uptime too.

Any suggestions?

Thanks!


From the html you have given, you could do the following:

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)

Giving you:

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

i.e mac_ips would hold each row as a matching pair:

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


If you want to separate lists then you could do the following:

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

Giving you:

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

Note: If you are parsing more html, then you will probably also need to first find the enclosing <table>.