2017-05-21 1 views
0

나는 내가 <p lang="title"> Notice how it has <sup></sup> and <sub></sub> tags being used inside.이 xpath 표현식에서 내부 HTML 컨텐트를 얻으려면 어떻게해야합니까?

내 XPath 식 .// 페이지를 추출하기 위해 노력하고, 위의 HTML 코드에서 일부 HTML 코드

<li><h3>Number Theory - Even Factors</h3> 
    <p lang="title">Number N = 2<sup>6</sup> * 5<sup>5</sup> * 7<sup>6</sup> * 10<sup>7</sup>; how many factors of N are even numbers?</p> 
    <ol class="xyz"> 
     <li>1183</li> 
     <li>1200</li> 
     <li>1050</li> 
     <li>840</li> 
    </ol> 
    <ul class="exp"> 
     <li class="grey fleft"> 
      <span class="qlabs_tooltip_bottom qlabs_tooltip_style_33" style="cursor:pointer;"> 
      <span> 
       <strong>Correct Answer</strong> 
        Choice (A).</br>1183 
       </span> 
       Correct answer 
      </span> 
     </li> 
     <li class="primary fleft"> 
      <a href="factors_6.shtml">Explanatory Answer</a> 
     </li> 
     <li class="grey1 fleft">Factors - Even numbers</li> 
     <li class="orange flrt">Medium</li> 
    </ul>  
</li> 

이/텍스트 [LANG = "제목"@]() 하위 및 sup 내용을 검색하지 않습니다. 내가 원하는 출력

다음이 출력을 얻을 어떻게

Number N = 2<sup>6</sup>*5<sup>5</sup> * 7<sup>6</sup> * 10<sup>7</sup>; how many factors of N are even numbers? 
+0

질문? –

+0

https://stackoverflow.com/questions/11744465/xpath-difference-between-node-and-text를 참조하십시오. –

답변

0

XPath를

당신은 단순히 얻을 수 아래 node()innerHTML :

//p[@lang="title"]/node() 

참고 그것은 그 배열을 반환합니다. 노드

파이썬

당신이 필요 얻을 수있는 innerHTML와 코드 Python 아래

from BeautifulSoup import BeautifulSoup 

def innerHTML(element): 
    "Function that receives element and returns its innerHTML" 
    return element.decode_contents(formatter="html") 

html = """<html> 
       <head>... 
       <body>... 
       Your HTML source code 
       ...""" 

soup = BeautifulSoup(html) 
paragraph = soup.find('p', { "lang" : "title" }) 

print(innerHTML(paragraph)) 

출력 :

언어는 XPath를 HTML을 구문 분석하고 실행하는 데 사용하는
'Number N = 2<sup>6</sup> * 5<sup>5</sup> * 7<sup>6</sup> * 10<sup>7</sup>; how many factors of N are even numbers?' 
관련 문제