2016-08-29 2 views
0

각 행에 두 개의 셀/열이있는 div 테이블이 있습니다. 두 번째 셀/열은 때로는 일반 텍스트 (<div class="something">Text</div>)를 가지며, 때로는 <div class="something"><a href="url">Text</a></div> 내부의 "a"태그 내에 숨겨져 있습니다.DIV 또는 기본 컨테이너가있는 경우 텍스트를 선택하십시오.

이제 링크 된 텍스트가 아닌 모든 것을 가져 오는 데 아무런 문제가 없습니다. 또한 링크 된 텍스트를 별도로 가져올 수도 있지만 모든 것을 한 번에 가져 오는 방법을 모르므로 데이터의 세 열을 가져옵니다. 1. 첫 번째 열 텍스트 2. 두 번째 열 텍스트는 연결되어 있는지 여부에 관계없이

times = scrapy.Selector(response).xpath('//div[contains(concat(" ", normalize-space(@class), " "), " time ")]/text()').extract() 
titles = scrapy.Selector(response).xpath('//div[contains(concat(" ", normalize-space(@class), " "), " name ")]/text()').extract() 
for time, title in zip(times, titles): 
    print time.strip(), title.strip() 

내가 할 수있는 단지로 링크 된 항목을 얻을

ltitles = scrapy.Selector(response).xpath('//div[contains(concat(" ", normalize-space(@class), " "), " name ")]/a/text()').extract() 
for ltitle in ltitles: 
    print ltitle.strip() 

그러나 돈 '아니, 3. 링크, 그것은

연결되지 모든 것을 추출하여 작동하는 코드가되어있는 경우 t를 결합하는 방법을 안다. 그는 모든 것을 함께 얻기 위해 "질의"를합니다.

<div class="programRow rowOdd"> 
    <div class="time ColorVesti"> 
         22:55 
    </div> 
    <div class="name"> 

         Dnevnik 

    </div> 
</div> 

<div class="programRow rowEven"> 
    <div class="time ColorOstalo"> 
         23:15 
    </div> 

    <div class="name"> 
    <a class="recnik" href="/page/tv/sr/story/20/rts-1/2434373/kulturni-dnevnik.html" rel="/ajax/storyToolTip.jsp?id=2434373">Kulturni dnevnik</a> 
    </div> 

</div> 

샘플 출력 (I 얻을 수없는 일) : 여기

은 샘플 HTML의

22:55, Dnevnik, [] 
23:15, Kulturni dnevnik, /page/tv/sr/story/20/rts-1/2434373/kulturni-dnevnik.html 

I 중 하나 (링크 텍스트없이) 처음 두 열하거나 링크 된 텍스트를 가져 위의 코드 샘플을 사용하십시오.

+0

당신은 샘플 입력 HTML을 공유하고 출력으로 얻을 원하는 것을 설명 할 수 있습니까? –

+0

@paultrmbrth : 완료! – illuminated

답변

0

정확하게 이해했다면 프로그램 노드를 반복하고 매주기마다 항목을 만들어야합니다. 또한이 XPath는 바로 가기 노드에서 모든 텍스트를 캡처 //text() 그리고 그것은

같은 것을 시도 childrem입니다 :

programs = response.xpath("//div[contains(@class,'programRow')]") 
for program in programs: 
    item = dict() 
    item['name'] = program.xpath(".//div[contains(@class,'name')]//text()").extract_first() 
    item['link'] = program.xpath(".//div[contains(@class,'name')]/a/@href").extract_first() 
    item['title'] = program.xpath(".//div[contains(@class,'title')]//text()").extract_first() 
    return item 
관련 문제