2014-02-19 16 views
0

嘗試解析站點上託管的xml文件中的信息。我正在爲xbmc製作一個電視插件,我的問題是信息全部在頁面上,我只想解析像第一季的所有部分!它只在一個地方顯示第一季,然後是第二季下方的所有情節。我不確定如何編寫它的代碼類型,以便只在第1季點擊時才拉動第1季!以下是我得到的:從xml文件解析python中的字符串

if type == 'tv_seasons': 
     match=re.compile('<Season no="(.+?)">').findall(content) 
     for seasonnumber in match:     
      item_url = new_url 
      item_title = 'Season ' + seasonnumber 
      item_id = common.CreateIdFromString(title + ' ' + item_title)    
      self.AddContent(list, indexer, common.mode_Content, item_title, item_id, 'tv_episodes', url=item_url, name=name, season=seasonnumber) 

    elif type == 'tv_episodes': 
     from entertainment.net import Net 
     net = Net() 
     content2 = net.http_GET(url).content 
     match=re.compile('<episode><epnum>.+?</epnum><seasonnum>(.+?)</seasonnum>.+?<link>(.+?)</link><title>(.+?)</title>').findall(content2) 
     for item_v_id_2, link_url, item_title in match: 
      item_v_id_2 = str(int(item_v_id_2)) 
      item_url = link_url 
      item_id = common.CreateIdFromString(name + '_season_' + season + '_episode_' + item_v_id_2) 
      self.AddContent(list, indexer, common.mode_File_Hosts, item_title, item_id, type, url=item_url, name=name, season=season, episode=item_v_id_2) 

所以,現在我正在與此工作,但仍然沒有爲我工作。

 tree2 = ET.parse(urllib.urlopen(url)) 
     root2 = tree2.getroot() 
     seasonnum = root2.findall("Show/Episodelist/Season[@no='%s']/episode/seasonnum" % season) 
     seasonnumtext = seasonnum.text 
     title = root2.findall("Show/Episodelist/Season[@no='%s']/episode/title" % season) 
     item_title = title.text 
     item_v_id_2 = str(int(seasonnumtext)) 
     item_url = url 
     item_id = common.CreateIdFromString(name + '_season_' + season + '_episode_' + item_v_id_2) 
     self.AddContent(list, indexer, common.mode_File_Hosts, item_title, item_id, type, url=item_url, name=name, season=season, episode=item_v_id_2) 
+4

're'實際上並不是xml的最佳工具。有一些專門的解決方案,該https://wiki.python.org/moin/PythonXml – njzk2

+0

請爲您的問題添加相關的一段html –

+0

http://services.tvrage.com/myfeeds/search.php?key = ag6txjP0RH4m0c8sZk2j&show = black%20sails這裏是xml文件 – Mikewave

回答