2017-08-30 59 views
2

我想抓scrapy this使用scrapy的网站。页面结构如下:如何选择和提取两个元素之间的文本?

<div class="list"> 
    <a id="follows" name="follows"></a> 
<h4 class="li_group">Follows</h4> 
<div class="soda odd"><a href="...">Star Trek</a></div> 
<div class="soda even"><a href="...</a></div> 
<div class="soda odd"><a href="..">Star Trek: The Motion Picture</a></div> 
<div class="soda even"><a href="..">Star Trek II: The Wrath of Khan</a></div> 
<div class="soda odd"><a href="..">Star Trek III: The Search for Spock</a></div> 
<div class="soda even"><a href="..">Star Trek IV: The Voyage Home</a></div> 
    <a id="followed_by" name="followed_by"></a> 
<h4 class="li_group">Followed by</h4> 
<div class="soda odd"><a href="..">Star Trek V: The Final Frontier</a></div> 
<div class="soda even"><a href="..">Star Trek VI: The Undiscovered Country</a></div> 
<div class="soda odd"><a href="..">Star Trek: Deep Space Nine</a></div> 
<div class="soda even"><a href="..">Star Trek: Generations</a></div> 
<div class="soda odd"><a href="..">Star Trek: Voyager</a></div> 
<div class="soda even"><a href="..">First Contact</a></div> 
    <a id="spin_off" name="spin_off"></a> 
<h4 class="li_group">Spin-off</h4> 
<div class="soda odd"><a href="..">Star Trek: The Next Generation - The Transinium Challenge</a></div> 
<div class="soda even"><a href="..">A Night with Troi</a></div> 
<div class="soda odd"><a href="..">Star Trek: Deep Space Nine</a></div 
</div> 

我想选择之间提取文本:<h4 class="li_group">Follows</h4><h4 class="li_group">Followed by</h4>然后<h4 class="li_group">Followed by</h4><h4 class="li_group">Spin-off</h4>
之间的文本我想这个代码:

def parse(self, response): 
    for sel in response.css("div.list"): 
     item = ImdbcoItem() 
     item['Follows'] = sel.css("a#follows+h4.li_group ~ div a::text").extract(), 
     item['Followed_by'] = sel.css("a#vfollowed_by+h4.li_group ~ div a::text").extract(), 
     item['Spin_off'] = sel.css("a#spin_off+h4.li_group ~ div a::text").extract(), 
    return item 

但是这个第一个项目提取的所有div不仅仅是div的<h4 class="li_group">Follows</h4><h4 class="li_group">Followed by</h4>
之间的任何帮助真的会Helpfu升!

+0

只是它帮助的情况下,imdb.com有一个(UN)官方的API在哪里?如果我记得好的话,你可以把所有这些数据清理干净。 – Neil

回答

2

我喜欢使用对于这些情况的提取图案是:

  • 环过来的 “边界”(这里,h4元素)
  • 而列举它们从1
  • 开始
  • 使用XPath的following-sibling轴,就像在@Andersson的答案中一样,在下一个边界之前获取元素,
  • 和通过计算前面的“边界”的元素个数过滤它们,因为我们从枚举知道我们在哪里

这将是循环:

$ scrapy shell 'http://www.imdb.com/title/tt0092455/trivia?tab=mc&ref_=tt_trv_cnn' 
(...) 
>>> for cnt, h4 in enumerate(response.css('div.list > h4.li_group'), start=1): 
...  print(cnt, h4.xpath('normalize-space()').get()) 
... 
1 Follows  
2 Followed by  
3 Edited into  
4 Spun-off from  
5 Spin-off  
6 Referenced in  
7 Featured in  
8 Spoofed in  

这是使用的一个例子枚举得到边界之间的元素(注意,在表达$cnt并通过cnt=cnt.xpath()这种使用XPath的变量):

>>> for cnt, h4 in enumerate(response.css('div.list > h4.li_group'), start=1): 
...  print(cnt, h4.xpath('normalize-space()').get()) 
...  print(h4.xpath('following-sibling::div[count(preceding-sibling::h4)=$cnt]', 
         cnt=cnt).xpath(
          'string(.//a)').getall()) 
... 
1 Follows  
['Star Trek', 'Star Trek: The Animated Series', 'Star Trek: The Motion Picture', 'Star Trek II: The Wrath of Khan', 'Star Trek III: The Search for Spock', 'Star Trek IV: The Voyage Home'] 
2 Followed by  
['Star Trek V: The Final Frontier', 'Star Trek VI: The Undiscovered Country', 'Star Trek: Deep Space Nine', 'Star Trek: Generations', 'Star Trek: Voyager', 'First Contact', 'Star Trek: Insurrection', 'Star Trek: Enterprise', 'Star Trek: Nemesis', 'Star Trek', 'Star Trek Into Darkness', 'Star Trek Beyond', 'Star Trek: Discovery', 'Untitled Star Trek Sequel'] 
3 Edited into  
['Reading Rainbow: The Bionic Bunny Show', 'The Unauthorized Hagiography of Vincent Price'] 
4 Spun-off from  
['Star Trek'] 
5 Spin-off  
['Star Trek: The Next Generation - The Transinium Challenge', 'A Night with Troi', 'Star Trek: Deep Space Nine', "Star Trek: The Next Generation - Future's Past", 'Star Trek: The Next Generation - A Final Unity', 'Star Trek: The Next Generation: Interactive VCR Board Game - A Klingon Challenge', 'Star Trek: Borg', 'Star Trek: Klingon', 'Star Trek: The Experience - The Klingon Encounter'] 
6 Referenced in  
(...) 

这里是你如何可以用它来填充和项目(在这里,我用一个简单的字典只是为了举例说明):

>>> item = {} 
>>> for cnt, h4 in enumerate(response.css('div.list > h4.li_group'), start=1): 
...  key = h4.xpath('normalize-space()').get().strip() # there are some non-breaking spaces 
...  if key in ['Follows', 'Followed by', 'Spin-off']: 
...   values = h4.xpath('following-sibling::div[count(preceding-sibling::h4)=$cnt]', 
...      cnt=cnt).xpath(
...       'string(.//a)').getall() 
...   item[key] = values 
... 

>>> from pprint import pprint 
>>> pprint(item) 
{'Followed by': ['Star Trek V: The Final Frontier', 
       'Star Trek VI: The Undiscovered Country', 
       'Star Trek: Deep Space Nine', 
       'Star Trek: Generations', 
       'Star Trek: Voyager', 
       'First Contact', 
       'Star Trek: Insurrection', 
       'Star Trek: Enterprise', 
       'Star Trek: Nemesis', 
       'Star Trek', 
       'Star Trek Into Darkness', 
       'Star Trek Beyond', 
       'Star Trek: Discovery', 
       'Untitled Star Trek Sequel'], 
'Follows': ['Star Trek', 
      'Star Trek: The Animated Series', 
      'Star Trek: The Motion Picture', 
      'Star Trek II: The Wrath of Khan', 
      'Star Trek III: The Search for Spock', 
      'Star Trek IV: The Voyage Home'], 
'Spin-off': ['Star Trek: The Next Generation - The Transinium Challenge', 
       'A Night with Troi', 
       'Star Trek: Deep Space Nine', 
       "Star Trek: The Next Generation - Future's Past", 
       'Star Trek: The Next Generation - A Final Unity', 
       'Star Trek: The Next Generation: Interactive VCR Board Game - A ' 
       'Klingon Challenge', 
       'Star Trek: Borg', 
       'Star Trek: Klingon', 
       'Star Trek: The Experience - The Klingon Encounter']} 
>>> 
+0

谢谢,作品像魅力。但我无法弄清楚如何在我的代码中使用它。你能给我一个提示或提供给我一个代码来使用吗? – haben

+0

看到我编辑的答案。 –

3

您可以尝试使用下面的XPath表达式为获取

  • 所有文本节点 “跟随” 块: “其次是” 块

    //div[./preceding-sibling::h4[1]="Follows"]//text() 
    
  • 所有文本节点:

    //div[./preceding-sibling::h4[1]="Followed by"]//text() 
    
  • “分离”块的所有文本节点:

    //div[./preceding-sibling::h4[1]="Spin-off"]//text() 
    
+1

你甚至可以简化'[./preceding-sibling::h4[1] [。=“Follows”]]'到'[./preceding-sibling::h4[1] =“Follows”]' –

+0

是的,有道理。谢谢 – Andersson

+0

你真是太棒了先生安德森。什么表达!是否有可能创建具有相同元素的CSS选择器来定位相同的东西?一个例子就足够了。谢谢。 – SIM

相关问题