2016-12-17 9 views
0

私は、治療のドキュメントの "チュートリアル"の後に引用符のタイトルを抽出しました。問題は、タイトルの最初と最後に2つのユニコードがあることです。ドキュメントでScrapy "引用チュートリアル" - 抽出されたテキストのUnicode

>>>quote = response.css("div.quote")[0] 
>>> quote 
<Selector xpath=u"descendant-or-self::div[@class and contains(concat(' ', normalize-space(@class), ' '), ' quote ')]" data=u'<div class="quote" itemscope itemtype="h'> 
>>> title = quote.css("span.text::text").extract_first() 
>>> title 
u'\u201cThe world as we have created it is a process of our thinking. It cannot be changed without changing our thinking.\u201d' 
>>> 

抽出されたタイトルは、次のようになります。

>>>title 
'"The world as we have created it is a process of our thinking. It cannot be changed without changing our thinking."' 
>>> 

I`m私はここで間違って何をしたかわからない、ただのドキュメントを追いました。設定ファイルに何か設定する方法がありますか、これをどのように修正できますか? ユニコードのデコード/エンコードについて何も言及されていません。例

その他

私はここで、scrapyのドキュメントと続けた別の例である:

Scrapyシェルの入力:

>>> for quote in response.css("div.quote"): 
...  text = quote.css("span.text::text").extract_first() 
...  author = quote.css("small.author::text").extract_first() 
...  tags = quote.css("div.tags a.tag::text").extract() 
...  print(dict(text=text, author=author, tags=tags)) 

出力スニペット:

{'text': u'\u201cTry not to become a man of success. Rather become a man of value.\u201d', 'tags': [u'humor', u'obvious', u'simile'], 'author': u'Albert Einstein'} 
{'text': u'\u201cIt is better to be hated for what you are than to be loved for what you are not.\u201d', 'tags': [u'humor', u'obvious', u'simile'], 'author': u'Albert Einstein'} 
{'text': u"\u201cI have not failed. I've just found 10,000 ways that won't work.\u201d", 'tags': [u'humor', u'obvious', u'simile'], 'author': u'Albert Einstein'} 
{'text': u"\u201cA woman is like a tea bag; you never know how strong it is until it's in hot water.\u201d", 'tags': [u'humor', u'obvious', u'simile'], 'author': u'Albert Einstein'} 
{'text': u'\u201cA day without sunshine is like, you know, night.\u201d', 'tags': [u'humor', u'obvious', u'simile'], 'author': u'Albert Einstein'} 

サイトiを掻き取った:

[http://quotes.toscrape.com]

ドキュメントScrapy(P.20):

https://media.readthedocs.org/pdf/scrapy/1.2/scrapy.pdf

システム:

MacOSのダーウィンカーネルバージョン16.3.0:木11月17日午後8時23分58秒PST 2016;ルート:XNU-3789.31.2〜1/RELEASE_X86_64

virtualenvののscrapy のPython 2.7.10

更新

は、私は、Python 3.5で新たにvirtualenvのののPython 3.5.2 と同じことを試してみました。 2私は最終的に、他の設定のようなユニコードの問題なしに正しい結果を得ます。

答えて

1

あなたが見ているのは、変数をインタプリタで表示するのではなく、その変数を表示しているだけなので、文字列のデバッグ表現です。 Python 2.7では、印刷できないすべての非ASCII文字がエスケープコードで表示されます。 Python 3では、現在の端末エンコーディングで表示可能な文字だけがエスケープコードとして表示されます。

文字を強制的に表示して文字を表示します。

>>> s=u'\u201cThe world\u201d' 
>>> s 
u'\u201cThe world\u201d' 
>>> print s 
“The world” 

あなたは印刷する端末は非ASCII文字をサポートしていませんが、あなたのためのPython 3.5動作するので、あなたの端末がそれらをサポートしなければならないエンコーディングを使用している場合UnicodeEncodeErrorを得ることができます。

デバッグ表示には、Unicode文字列を表すuも表示され、出力も引用符で囲まれています。 printは文字列の内容を表示するだけです。

+0

ありがとうございます;) – Luca

関連する問題