2016-08-04 20 views
0

我的代碼無法正常工作。如何使用scrapy提取兩級文本?

第二個for循環沒有獲取所有文本。

我該如何在scrapy中做到這一點?

感謝您的任何提示,讓我知道如果我失去了什麼。

<dl> 
<dt>Release Date:</dt> 
<dd>Aug. 01, 2016<br> 
</dd> 

<dt>Runtime:</dt> 
<dd itemprop="duration">200min.<br></dd> 

<dt>Languages:</dt> 
<dd>Japanese<br></dd> 
<dt>Subtitles:</dt> 
<dd>----<br></dd> 
<dt>Content ID:</dt> 
<dd>8dtkm00045<br></dd> 
<dt>Actress(es):</dt> 
<dd itemprop="actors"> 
    <span itemscope="" itemtype="http://schema.org/Person"> 
     <a itemprop="name">Shinobu Oshima</a> 
    </span> 

    <span itemscope="" itemtype="http://schema.org/Person"> 
     <a itemprop="name">Yukie Mizukami</a> 
    </span> 

</dd> 

蜘蛛:

def parse_item(self, response): 
    for sel in response.xpath('//*[@id="contents"]/div[10]/section/section[1]/section[1]'): 
     item = EnMovie() 
     Content_ID = sel.xpath('normalize-space(div[2]/dl/dt[contains (.,"Content ID:")]/following-sibling::dd[1]/text())').extract() 
     item['Content_ID'] = Content_ID[0].encode('utf-8') 
     release_date = sel.xpath('normalize-space(div[2]/dl[1]/dt[contains (.,"Release Date:")]/following-sibling::dd[1]/text())').extract() 
     item['release_date'] = release_date[0].encode('utf-8') 
     running_time = sel.xpath('normalize-space(div[2]/dl[1]/dt[contains (.,"Runtime:")]/following-sibling::dd[1]/text())').extract() 
     item['running_time'] = running_time[0].encode('utf-8') 
     Series = sel.xpath('normalize-space(div[2]/dl[2]/dt[contains (.,"Series:")]/following-sibling::dd[1]/text())').extract() 
     item['Series'] = Series[0].encode('utf-8') 
     Studio = sel.xpath('normalize-space(div[2]/dl[2]/dt[contains (.,"Studio:")]/following-sibling::dd[1]/a/text())').extract() 
     item['Studio'] = Studio[0].encode('utf-8') 
     Director = sel.xpath('normalize-space(div[2]/dl[2]/dt[contains (.,"Director:")]/following-sibling::dd[1]/text())').extract() 
     item['Director'] = Director[0].encode('utf-8') 
     Label = sel.xpath('normalize-space(div[2]/dl[2]/dt[contains (.,"Label:")]/following-sibling::dd[1]/text())').extract() 
     item['Label'] = Label[0].encode('utf-8') 
     item['image_urls'] = sel.xpath('div[1]/img/@src').extract() 


     for actress in sel.xpath("//*[@itemprop='actors']//*[@itemprop='name']"): 
      actress_ = actress.xpath("text()").extract() 
      item['Actress'] = actress_[0].strip() 
      yield item 

部分蜘蛛工作良好(除第二個for循環)第二個for循環產率只有最後[itemprop = 「名稱」]值並保存到DB。

對不起,我的英語不好,謝謝你的提示。

+0

第二個'for'循環中第一行前面的額外空間是否有問題? – mitoRibo

+0

那麼,你需要爲每個女演員分開一個EnMovie項目嗎?或者你需要一個列表與電影裏的所有女演員的名單? –

+0

我需要爲每個女演員分開一個EnMovie項目! – Jin

回答

0

與此更換你的第二個循環:

actresses = sel.xpath("//*[@itemprop='actors']//*[@itemprop='name']/text()").extract() 

item['Actress'] = [x.strip() for x in actresses] 

yield item 

它會給其中有女演員的一個列表中的項目。

BYW,請停止發佈同樣的問題againagain並再次發佈。