2016-12-17 51 views
0

我按照scrapy文档中的“tutorial”提取了引用的标题。问题是,它在标题的开头和结尾处给了我两个unicode。Scrapy“Quotes Tutorial” - 提取文本中的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."' 
>>> 

我真的不知道我做错了什么在这里,只是跟着的文档。有没有在配置文件中设置的东西,或者我该如何解决这个问题? 关于解码/编码unicode没有提及。

其他实施例

我接着与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'} 

网站我从:

[http://quotes.toscrape.com]

文档Scrapy(第20页):

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

系统:

MACOS达尔文内核版本16.3.0:星期四11月17日20时23分58秒PST 2016年根:XNU-3789.31.2〜1/RELEASE_X86_64

的virtualenv scrapy 的Python 2.7.10

更新

我试图在同一用新的virtualenv的Python 3.5.2 使用Python 3.5。 2我终于得到了正确的结果,没有像其他设置中的unicode问题。

回答

1

您所看到的是字符串的调试表示形式,因为您只是在解释器中查看变量而不是打印它。在Python 2.7中,所有不可打印的非ASCII字符都使用转义码显示。在Python 3中,只有当前终端编码中可显示的字符会显示为转义码。

打印字符串以强制显示字符。

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

可以得到UnicodeEncodeError如果打印终端使用不支持非ASCII字符的编码,但因为Python 3.5为你的作品,你的终端必须支持他们。

请注意,调试显示屏还显示代表Unicode字符串的u并引用输出。 print只是显示字符串内容。

+0

谢谢您的回答;) – Luca