2016-12-24 8 views
1

ここに私のコードスニペットです。 Scrapyを使用してウェブサイトを掻き集め、インデックス作成のためにElasticsearchにデータを保存しています。Scrapy:レスポンスをクリーンアップするには?

def parse(self, response): 
    for news in response.xpath('head'): 
     yield { 
      'pagetype': news.xpath('//meta[@name="pagetype"]/@content').extract(), 
      'description': news.xpath('//div[@class="module__content"]/*/node()/text()').extract(), 
       } 

私の問題は「説明」フィールドに保存される値です。私は、さらにちょうど余分な空白、改行のない通常のテキストを、含まれているために、このコードを処理するにはどうすればよいの空白がたくさん、改行コードと「U」の文字があります

[u'\n    \n    ', u'"For\n    many of us what we eat on Christmas day isn\'t what we would usually consume and\n    that\u2019s perfectly ok," Dr said.', u'"However\n    it is not uncommon for festive season celebrations to begin in November and\n    continue well in to the New Year.', u'"So\n    if health is on the agenda, being mindful about what we put into our bodies\n    with a balanced approach, throughout the whole festive season, is important."', u"Dr\n    , a lecturer at School\n    Sciences, said balancing fresh, healthy food with being physically active was a\n    good start.", u'"Whatever\n    the celebration, try to limit processed foods, often high in fat, sugar and\n    salt," she said.', u'"Taking\n    time during holidays to prepare food and make the most of fresh ingredients is\n    often a much healthier option than relying on convenience foods and take away.', u'"Being\n    mindful about going back for seconds is important too.\xa0 We don\u2019t need to eat until we feel\n    uncomfortable and eating the foods we enjoy doesn\'t necessarily mean we need to\n    eat copious amounts."', u"Dr\n    own healthy tips and substitutes for the Christmas season\n    include:", u'But\n    just because Dr is a dietitian, doesn\u2019t mean she doesn\u2019t enjoy a\n    Christmas treat or two.', u'"I\n    would have to say my sister in law\'s homemade rocky road is my favourite\n    festive treat. She makes it every Christmas day and it gets better each year," she\n    said.', u'"I\n    also enjoy a summer cocktail every so often during the festive season and a\n    mojito would be one of my favourites on Christmas day. We make it with extra\n    mint from the garden which is a nice, fresh addition.', u'"Rather\n    than focusing on food avoidance, moderation is the best approach.', u'"There\n    are definitely some more healthy choices and some less healthy options when it\n    comes to the typical Christmas day menu, but it\'s more important to be mindful\n    of a healthy, balanced diet throughout the festive period, rather than avoiding\n    specific foods on one day of the year."', u'\n    ', u'\n    \n    ', u'\n    ', u'\n    \n    ', u'\n    ', u'\n    ', u'\n      ', u'\n      ', u'\n      ', u'\n     ', u'\n   ', u'Related News', u'\n   ', u'\n  ', u'\n   ', u'\n  ', u'\n   ', u'\n  ', u'Search for related news'] 

....

(\ nは)コードと 'u'文字?

私はそれを読んでBeautifulSoupはScrapyとうまく動作しますが、ScrapyとBeautifulSoupを統合する方法の例は見つかりませんでした。私は他の方法も使用しています。どんな助けもありがとうございます。

おかげ

+0

関連:http://stackoverflow.com/q/21839877/4063051 – glS

+0

「u」は、あなたがリスト上でユニコードのテキストを持っているという情報だけです。リストから1つの要素を印刷すると、 'u'のないテキストが表示される – furas

+0

、それらの文字列から改行と空白を削除したいだけですか? – glS

答えて

0

あなたは、例えばin this answerを示す方法を使用して、リスト内の文字列からスペースや改行を取り除くことができます。

[' '.join(item.split()) for item in list_of_strings] 

list_of_stringsは、あなたが一例として与えた文字列のリストです。

「u文字」については、本当に心配するべきではありません。 文字列がUnicodeエンコーディングであることを意味します。例えば、問題にthis question

+0

ありがとう、どうやってこれを使うの?私はScrapyシェルでこれを実行しました – Slyper

+0

ありがとう、どうやって使うの?私はScrapyシェルでこれを実行しました '' .join(myString.split()) _AttributeError: 'list'オブジェクトには属性 'split'_ – Slyper

+0

がありません。質問に入れた文字列のリストを保存した場合変数 'list_of_string'として、単に上記の行を実行し、空白と改行を取り除いた要素で同じリストを取得します。 – glS

関連する問題