Thank you for your reply Gerk. I have separated the code as you suggested and found a better anchor for my xpath.
However, now I am running into another error. Unfortunately the site lists upcoming episodes that have no video. Obviously I only care about those episodes with video. So I came up with the code below. When I run it, I get every episode (with or without video) and it seems that my code only grabs the first instance of an URL and uses that for each episode.
So I'm not sure what is going wrong here. If I use epURL = item.xpath('./span[contains(@class, "vid")] I get no episodes and if I use epURL = item.xpath('//span[contains(@class, "vid")] I get every episode with or without video. Neither is what I expect.
I hope this makes sense.
To illustrate, take this example: http://ww3.tvo.org/program/201110
Using "//", I get all 6 episodes in my list and all point to the URL for episode #4 (the first instance of an URL in this case).
Here is my code:
###################################################################################################
@route(PREFIX + '/showepisodes')
def ShowEpisodes(title, pass_url, showpg):
oc = ObjectContainer(title2=title)
pageElement = HTML.ElementFromURL(pass_url)
for item in pageElement.xpath('//li[contains(@class, "views-row views-row")]'):
try:
epURL = item.xpath('//span[contains(@class, "vid")]/a[contains(@class, "watch-video")]')[0].get('href')
epTitle = item.xpath('./span[contains(@class, "ep-title")]')[0].text
oc.add(DirectoryObject(key=Callback(PlayEpisodes, title=epTitle, pass_url=epURL), title=epTitle, summary='Add summary'))
Log(epTitle)
Log(epURL)
except:
continue
return oc
Here is a snippet from the website:
Episode 6
It’s harvest time, winter is drawing in and the Dissolution of the Monasteries is on the horizon. This week the team will be bringing in the barley and celebrating with a harvest feast, to give thanks for their bounteous crop.
Episode 5
This episode explores the theme of hospitality in Tudor England. With no provision for the poor from the state, it was down to the monasteries to provide welfare for those in need.
Episode 4
This week the team learn to master the landscape away from the farm in order to supplement their income. The monasteries’ land covered a variety of landscapes, from rivers and woodlands to hills and mines, all of which would be expected to be exploited by the tenant farmer to raise income for themselves and the monastery.