2013-04-26 4 views
1

나는 다음과 같은 태그 분할하는 것을 시도하고있다 : 다음 코드를 사용하여파이썬 분할 문자열 인덱스

<h3><a href="#AC Adapter" onclick="getProductsBasedOnCategoryID('Asus','AC Adapter','ET1611PUT','6941', this, 'E Series')">AC Adapter 

      </a></h3> 

을 그러나

print "FETCHING CATEGORY" 
    atag = s.h3 
    for data in atag: 
     while getattr(atag, 'name', None) != 'h3': 
      atag = atag.nextSibling 
     atag.a 
     atag = literal_eval('(' + atag.nextSibling.replace(', this', '').split('(', 1)[1]) 
     print atag 

을, 나는 다음과 같은 오류 얻을 :

File "//CPSBS/RedirectedFolders/aysha/My Documents/asus_tables(edited) a tags.py", line 84, in <module> 
    atag = literal_eval('(' + atag.nextSibling.replace(', this', '').split('(', 1)[1]) 
IndexError: list index out of range 

나는 뭔가 잘못하고있는 것으로 추측하고 있습니까? 또한이 a 태그는 onclick 속성을 가지고 있는데 대신 액세스하고 싶습니다. 그러면 다음 코드에 어떻게 입력합니까? 여기

내가 시리즈

한/E에서

http://www.asusparts.eu/partfinder/Asus/All에서 데이터를 분석하고있는 URL입니다

[편집]

탐색 트리 내가

에서 데이터를 검색하는 것을 시도하고있다
<div id="accordion" class="ui-accordion ui-widget ui-helper-reset ui-accordion-icons" style="width: 760px;" role="tablist"> 
    <h3 class="ui-accordion-header ui-helper-reset ui-state-active ui-corner-top" role="tab" aria-expanded="true" aria-selected="true" tabindex="0"> 
     <span class="ui-icon ui-icon-triangle-1-s"></span> 
     <a onclick="getProductsBasedOnCategoryID('Asus','AC Adapter','ET10B','6941', this, 'E Series')" href="#AC Adapter" tabindex="-1" loaded="Loaded">AC Adapter </a> 
    </h3> 
    <div id="6941" class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active" role="tabpanel" style="display: block;"> 
     <table class="productTableList"> 
      <tbody> 
     </table> 
     <table class="productTableList"> 
      <tbody> 
       <tr style="height:90px;background-color:#ebf4ff;"> 
        <td class="ProduktLista" width="70px"> 
        <td class="ProduktLista" width="315"> 
         <a onclick="getProductInformationModal("Asus","14G110008340");"> 
         <br> 
+0

코드를 실행할 수 있기 때문에 오류를 표시하는 대신, 해당 평가를 여러 개별 행으로 나누지 않고 무엇이 반환되는지 확인하십시오. ''분할' '. 처음 시도하면 도움이 될 것입니다. 또는 적어도 그 일을 한 후에 얻은 것을 게시하십시오. – CppLearner

답변

1

이러한 유형의 문제에 직면했을 때 즉시 문제를 볼 수 없습니다. 그렇다면 복잡한 표현을 나눌 필요가 있습니다. 대신에 :

nextSibling = atag.nextSibling 
txt1 = nextSibling.replace(', this', '') 
split = txt1.split('(', 1) 
txt2 = split[1] 
txt3 = '(' + txt2 
atag = literal_eval(txt3) 

이 당신에게 문제가 존재 정확한 표현을 얻을 것이다, 그리고 :

atag = literal_eval('(' + atag.nextSibling.replace(', this', '').split('(', 1)[1]) 

에 재를 (당신은 물론, 변수 더욱 의미가 이름을 사용한다) 관련된 값의 print 문을 사용하면 대답을 얻을 수 있습니다.

+0

그것은 아무것도 인쇄하지 않습니다 :/위의 내 게시물을 편집하고 html의 탐색 트리 스 니펫을 추가했습니다. – ash