re-formatting HTML using python 2.7 and BS4
<p>I have a piece of software that exports race results as a htm file.
I currently open this file in excel and reformat the results tables into a format suitable to be uploaded into a mysql databse, where lots of number crunching takes place using php.<br><br>
I want to automate the re-format/parse/scrape, and searches on the web keep suggesting python 2.7 with beuatifulsoup4 which I started to play around with, but I have not used either before...<br></p>

<p>This is a look at the original table structure<br><br></p>

<pre><code>&lt;h1&gt;CLASS 1&lt;/h1&gt;
    &lt;table class="results"&gt;
        &lt;tr&gt;&lt;th nowrap="1"&gt;Name&lt;/th&gt;&lt;th nowrap="1"&gt;Town&lt;/th&gt;&lt;th nowrap="1"&gt;Bike&lt;/th&gt;&lt;th nowrap="1"&gt;Penalty&lt;/th&gt;&lt;/tr&gt;
        &lt;tr&gt;&lt;td class="rider"&gt;RIDER 1&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;01:14:20&lt;/td&gt;&lt;/tr&gt;
        &lt;tr&gt;&lt;td colspan="7"&gt;&lt;table class="laps"&gt;&lt;tr&gt;&lt;td&gt;00:26:36&lt;/td&gt;&lt;td&gt;00:19:51&lt;/td&gt;&lt;td&gt;00:27:54&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;&lt;/tr&gt;
        &lt;tr&gt;&lt;td class="rider"&gt;RIDER 2&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;00:41:06&lt;/td&gt;&lt;/tr&gt;
        &lt;tr&gt;&lt;td colspan="7"&gt;&lt;table class="laps"&gt;&lt;tr&gt;&lt;td&gt;00:19:10&lt;/td&gt;&lt;td&gt;00:21:57&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;&lt;/tr&gt;
        &lt;tr&gt;&lt;td class="rider"&gt;RIDER 3&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;00:36:59&lt;/td&gt;&lt;/tr&gt;
        &lt;tr&gt;&lt;td colspan="7"&gt;&lt;table class="laps"&gt;&lt;tr&gt;&lt;td&gt;00:37:00&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;&lt;/tr&gt;
        &lt;tr&gt;&lt;td class="rider"&gt;RIDER 4&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;01:26:41&lt;/td&gt;&lt;/tr&gt;
        &lt;tr&gt;&lt;td colspan="7"&gt;&lt;table class="laps"&gt;&lt;tr&gt;&lt;td&gt;01:26:42&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;&lt;/tr&gt;
    &lt;/table&gt;
&lt;h1&gt;CLASS 2&lt;/h1&gt;
</code></pre>

<p>I would like the export to be one table for each class with all the info for eash rider on a single row, like this...<br><br>
NAME1 02:26:4 200:12:42 00:13:04 00:13:25 00:13:19 00:13:22 00:13:29 00:13:44   <bR>
NAME2: 02:41:06 00:13:17 00:14:10 00:13:40 00:13:38 00:13:47 00:13:12 00:13:24<br><br>
Playing around in python I got so far as to reading file using beautifulsoup.</p>

<pre><code>from bs4 import BeautifulSoup

with open(r'test.htm', "r") as f:
    pagebuffer = f.read()  
soup = BeautifulSoup(pagebuffer, "lxml")
</code></pre>

<p>After inspecting the html I was able to search the soup for the relevant class names. </p>

<pre><code>riders = soup.find_all(class_="rider")
for item in riders:
    print item.text   
</code></pre>

<p>NAME 1<br>
NAME  2<br>
NAME  3<br>
NAME  4<br>
NAME  5<br>
NAME  6<br>
NAME  7 <br>
NAME  8<br>
NAME  9<br>
NAME  10<br>
NAME  11<br>
NAME  12<br>
NAME  13<br>
NAME  14<br>
NAME  15<br>
NAME  16<br>
NAME  17<br>
NAME  18<br>
NAME  19<br></p>

<pre><code>laps = soup.find_all(class_="laps")
for item in laps:
    print item.text  
</code></pre>

<p>00:12:4200:13:0400:13:2500:13:1900:13:2200:13:2900:13:4400:13:3000:13:2000:13:3800:13:10<br>
00:12:2600:13:1700:14:1000:13:4000:13:3800:13:4700:13:1200:13:2400:13:2500:13:4700:13:43<br>
00:12:3100:13:1300:13:2200:13:5200:13:5500:14:0800:13:2500:13:4500:13:5300:13:4400:13:25<br>
00:14:2300:14:2600:15:0100:14:5300:14:5800:14:3100:14:4400:15:3300:14:1900:14:14<br>
00:13:5700:13:4800:14:1900:14:3200:14:5100:15:0300:14:3600:17:5700:14:4200:14:39<br>
00:14:1100:14:3200:14:4300:14:2300:14:5900:14:4600:15:1000:15:0500:15:1400:16:13<br>
00:13:4100:13:3200:14:0000:14:0100:14:3200:14:1000:14:3600:14:2100:28:5500:14:17<br>
00:13:3000:13:3900:14:00<br>
<br>
<br>
02:36:4900:13:2800:13:3700:13:5600:13:5700:14:4600:14:1700:14:2700:15:1800:14:3800:14:1100:14:15<br>
02:36:5800:13:5900:13:4900:14:1900:14:1100:14:2300:14:2700:14:2700:14:2400:14:2600:14:1300:14:21<br>
02:27:0100:14:2300:14:2600:15:0100:14:5300:14:5800:14:3100:14:4400:15:3300:14:1900:14:14<br>
02:28:2300:13:5700:13:4800:14:1900:14:3200:14:5100:15:0300:14:3600:17:5700:14:4200:14:39<br>
02:29:1500:14:1100:14:3200:14:4300:14:2300:14:5900:14:4600:15:1000:15:0500:15:1400:16:13<br>
02:36:0400:13:4100:13:3200:14:0000:14:0100:14:3200:14:1000:14:3600:14:2100:28:5500:14:17<br>
00:41:0800:13:3000:13:3900:14:00<br>
<br>
<br></p>

<p>This is where I'm stuck...<br>
1. How do I combine those two searches (riders, laps)together?<br>
2. The total time is not defined by a class name, how can I search to the 3rd [td] tag after the class rider [td] tag?<br>
3. Will I be able to make this executable on machines that dont have python and bs4 installed or should i look at other coding methods?<br></p>

<p>here is a link to a typical htm file: <a href="http://www.kr3w.co.uk/downloads/test.htm" rel="nofollow">http://www.kr3w.co.uk/downloads/test.htm</a></p>