Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>from BeautifulSoup import BeautifulSoup import re #day input day ='Thu' #searches for a module (where html has rowspan="1") module = re.compile(r'rowspan=\"1\"') #lengths of module search (depending on html colspan attribute) #1.5 hour perlen15 = re.compile(r'colspan=\"3\"') #2 hour perlen2 = re.compile(r'colspan=\"4\"') #2.5 hour etc. perlen25 = re.compile(r'colspan=\"5\"') perlen3 = re.compile(r'colspan=\"6\"') perlen35 = re.compile(r'colspan=\"7\"') perlen4 = re.compile(r'colspan=\"8\"') #times correspond to first row of timetable. times = ['8:00', '8:30', '9:00', '9:30', '10:00', '10:30', '11:00', '11:30', '12:00', '12:30', '13:00', '13:30', '14:00', '14:30', '15:00', '15:30'] #opens full timetable html with open('timetable.txt', 'rt') as input_file: html = input_file.read() soup = BeautifulSoup(html) #finds correct day tag starttag = soup.find(text=day).parent.parent nexttag = starttag row=[] #movement of cursor iterating over times list curmv = 0 #puts following td tags for that day in a list for time in times: nexttag = nexttag.nextSibling.nextSibling #detect if a module is found found = module.search(repr(nexttag)) #detect length of that module hour15 = perlen15.search(repr(nexttag)) hour2 = perlen2.search(repr(nexttag)) hour25 = perlen25.search(repr(nexttag)) hour3 = perlen3.search(repr(nexttag)) hour35 = perlen35.search(repr(nexttag)) hour4 = perlen4.search(repr(nexttag)) if found: row.append(times[curmv]) row.append(nexttag) if hour15: curmv += 3 elif hour2: curmv += 4 elif hour25: curmv += 5 elif hour3: curmv += 6 elif hour35: curmv += 7 elif hour4: curmv += 8 else: curmv += 2 else: curmv += 1 #write day to html file with open('output.html', 'wt') as output_file: for e in row: output_file.write(str(e)) </code></pre> <p>as you can see, the code can differentiate between one hour and 2 hour long lectures as well as 1.5, 2.5 hour longs ones etc. </p> <p>my only problem now is line 32, I need a better way to tell the code to stop moving horizontally across the table aka: knowing when to stop the for loop (in the previous code I had <code>while x &lt; 18:</code> which only worked for monday because there were 18 td tags in the row. How can I get the loop to stop when it hits the parent <code>&lt;/tr&gt;</code> tag? </p> <p>thanks!</p> <p>EDIT: I'm going to try using a try and except block to catch the error I get if I set the 'times' set all the way up to 18:00.</p> <p>EDIT2: IT WORKED! :D</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload