2014-02-19 4 views
0

사이트에서 호스팅되는 XML 파일에서 정보를 구문 분석하려고합니다. xbmc에 대한 TV addon을 만들고 있는데 내 이슈는 정보가 페이지에 모두 들어 있고 모든 시즌 1과 같은 섹션에서만 구문 분석을 원한다는 것입니다! 한 곳에서 시즌 1을 보여 주며 시즌 2에서는 모든 에피소드를 보여줍니다. 시즌 1을 클릭하면 시즌 1을 끌어 올 수있는 코드 유형을 작성하는 방법을 모르겠습니다! 아래는 내가 무엇을 할 수 있습니다 :XML 파일에서 파이썬의 문자열 구문 분석

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

pls에 대한 몇 가지 전용 솔루션이 있습니다. 질문에 관련 HTML 조각을 추가하십시오. –

+0

http://services.tvrage.com/myfeeds/search.php?key = ag6txjP0RH4m0c8sZk2j & show = black % 20sails는 여기 XML 파일 – Mikewave

답변

2

Python XML Parser을 사용하는 것이 좋습니다. 그런 다음 Python 사전과 목록과 유사한 방법으로 XML 트리를 탐색 할 수 있습니다.