2017-02-10 3 views
0

저는 치료와 함께 간단한 긁는 도구를 만들었지 만 데이터의 특정 부분을 추출하는 데 문제가 있습니다. 나는 이러한 각각을 추출하는 데 성공했다부서에서 텍스트를 추출하는 치료

<div class="row result"> 
    <div class="updateCont date col-md-2 col-sm-2 col-xs-3"> 
     <span>  
      <strong>Fri. 10 Feb</strong> <br />0:00 AM 
     </span> 
    </div> 
    <div class="updateCont eventIcon col-md-1 col-sm-1 col-xs-3"> 
     <div class="icon "> 
      <i class="fa fa-update"></i> 
     </div> 
    </div> 
    <div class="updateCont event col-md-9 col-sm-8 col-xs-6"> 
     <span> 
       The buyer has been notified of this update. <br /> 
       <span class="inner department"> 
        124 
       </span> 
     </span> 
    </div> 
</div> 

: 웹 사이트는 약 20 코드의 다음 블록을 포함

sel = Selector(text=response.body) 
updates = sel.xpath("//div[@class='row result']") 
지금 날짜를 분리하고로 변환하고자하는

datetime 객체와 updateCont 이벤트 문자열을 반환합니다. 구입은이 업데이트로 통보되었습니다.

나는 시도 :

난 그냥 동일한 데이터를 3 회 인쇄 날짜를 인쇄하면 내가 더 worringly이 3 할 것으로 예상했다 7. 결과
for update in updates: 
     date = update.xpath('//span').extract() 
     print (len(date)) 

. 나는 html에서 3 개의 분리 된 데이터가 있기 때문에 3 개의 다른 많은 데이터를 기대하고 있었다.

sel = Selector(text=response.body) 
updates = sel.xpath("//div[@class='row result']") 

섹션을 분리하는 올바른 코드인가? 스팬을 추출하는 최선의 방법은 무엇입니까?

답변

-1
In [19]: for update in updates: 
    ...:   spans = update.xpath('//span') 
    ...:   for span in spans: 
    ...:    text = span.xpath('normalize-space()').extract_first() 
    ...:    print(text) 
    ...:    
    ...:  

아웃 :

Fri. 10 Feb 0:00 AM 
The buyer has been notified of this update. 124 
124 

사용 .는 현재 노드가 작동하는지에 대한

+0

감사를 분리합니다. 작은 문제는 이제'금입니다. 10 Feb
0:00 AM'은 강한 태그 내의 비트가 아닌 0:00 AM 만 추출합니다. –

+0

나는 아직도 강한 태그에 비트를 얻지 못하고있다. 일단 그것이 완전히 작동하면 물론 대답을 받아 들일 것입니다. –

관련 문제