2017-06-03 8 views
0

ScrapyのItemLoaderを使用して、HTML要素の最初のn文字を解析したいと思います。保持されるテキストの一部を構成するテキストを含む)。Scrapy ItemLoaderを使用して、複数の要素のテキストの最初のn文字を解析します。

は、ここで設定例です:

サンプルHTML:

<div class="about-copy"> 
<p>Developers trust Stack Overflow to help solve coding problems 
    and use Stack Overflow Jobs to find job opportunities. We’re 
    committed to making the internet a better place, and our products 
    aim to enrich the lives of developers as they grow and mature in 
    their careers. 
</p> 
<a href='...'></a> 
<p>Founded in 2008, Stack Overflow sees 40 million visitors each month 
    and is the flagship site of the Stack Exchange network, home to 150+ 
    Q&A sites dedicated to niche topics. 
</p> 
</div> 

パーサコード:

def parse_details(self, response): 
    ... 
    l = ItemLoader(item=Entry(), response=response) 
    # this is presumably the portion of the code that is to be modified 
    l.add_css('f_brief_summary', 'div.about-copy::text') 
    ... 

望ましい結果:

Developers trust Stack Overflow to help solve coding problems 
    and use Stack Overflow Jobs to find job opportunities. We’re 
    committed to making the internet a better place, and our products 
    aim to enrich the lives of developers as they grow and mature in 
    their careers. Founded in 2008, Stack Overflow 

ワンステップの方法はありますこれを行うにはItemLoaderを使用するか、または解析を手動で行う必要があります。次に、テキストを 'add_value'メソッドでItemLoadedオブジェクトに追加しますか?

答えて

1

汎用のItemLoaderを使用する代わりに、独自のLoaderクラスを作成します。次に、フィールドのそれぞれに前処理と後処理を適用したり、すべてのプロセッサを定義することができます。参照:Scrapy Item Loaders Guide

エントリ項目を定義するモジュールで、以下を追加します。以下の例では、セレクタで「:: text」ではなく「remove_tags」メソッドを使用しています。

import scrapy 
from scrapy.loader import ItemLoader 

from scrapy.loader.processors import MapCompose, Join 
from w3lib.html import remove_tags 


# You can do so much better here! 
def format_me(x): 
    return x.replace('\n', ' ').replace(' ', ' ').strip() 

# Here is the Loader you need to add; mine only covers one field. 
class EntryLoader(ItemLoader): 
    f_brief_summary_in = MapCompose(remove_tags, format_me) 
    f_brief_summary_out = Join() 

# You already have this; mine only covers one field. 
class Entry(scrapy.Item): 
    f_brief_summary = scrapy.Field() 

これにより、必要な結果が得られます。テストするには:

例のスニペットをファイルに保存します。 example.html

実行scrapyシェルシェルで

scrapy shell './example.html' 

があなたの項目およびLoaderインポート:

from scrapyproj.entry_module import EntryLoader, Entry 

テストをパーサ:

entry_loader = EntryLoader(item=Entry(), response=response) 
entry_loader.add_css('f_brief_summary', 'div.about-copy') 
entry_loader.load_item() 

出力:

{'f_brief_summary': 'Developers trust Stack Overflow to help solve coding ' 
        'problems and use Stack Overflow Jobs to find job ' 
        'opportunities. We’re committed to making the internet a ' 
        'better place, and our products aim to enrich the lives ' 
        'of developers as they grow and mature in their ' 
        'careers. Founded in 2008, Stack Overflow sees 40 ' 
        'million visitors each month and is the flagship site of ' 
        'the Stack Exchange network, home to 150+ Q&amp;A sites ' 
        'dedicated to niche topics.'} 
関連する問題