Scrapy "Quotes Tutorial" - 提取文本中的 Unicode
Scrapy "Quotes Tutorial" - Unicode in extracted text
我在 scrapy 文档中的 "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."'
>>>
我不确定我在这里做错了什么,只是按照文档进行操作。是否需要在配置文件中进行设置,或者我该如何解决?
没有提到 decode/encode unicode。
其他例子
我继续使用 scrapy 文档,这是另一个例子:
Scrapy Shell 输入:
>>> 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'}
我从中抓取的网站:
文档 Scrapy(第 20 页):
https://media.readthedocs.org/pdf/scrapy/1.2/scrapy.pdf
系统:
macOS Darwin 内核版本 16.3.0:11 月 17 日星期四 20:23:58 PST 2016; root:xnu-3789.31.2~1/RELEASE_X86_64
virtualenv scrapy
Python 2.7.10
更新
我用新的 virtualenv 做了同样的尝试 Python 3.5.2
使用 Python 3.5.2 我终于得到了正确的结果,没有像其他设置中那样的 unicode 问题。
您看到的是字符串的调试表示,因为您只是在解释器中查看变量而不是打印它。在 Python 2.7 中,所有不可打印的非 ASCII 字符都显示有转义码。在 Python 3 中,只有当前终端编码中可显示的字符才显示为转义码。
打印字符串以强制显示字符。
>>> s=u'\u201cThe world\u201d'
>>> s
u'\u201cThe world\u201d'
>>> print s
“The world”
如果您打印的终端使用的编码不支持非 ASCII 字符,您 可能 得到 UnicodeEncodeError
,但是由于 Python 3.5 适合你,你的终端必须支持它们。
请注意,调试显示还显示了代表 Unicode 字符串的 u
并引用了输出。 print
只显示字符串内容。
我在 scrapy 文档中的 "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."'
>>>
我不确定我在这里做错了什么,只是按照文档进行操作。是否需要在配置文件中进行设置,或者我该如何解决? 没有提到 decode/encode unicode。
其他例子
我继续使用 scrapy 文档,这是另一个例子:
Scrapy Shell 输入:
>>> 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'}
我从中抓取的网站:
文档 Scrapy(第 20 页):
https://media.readthedocs.org/pdf/scrapy/1.2/scrapy.pdf
系统:
macOS Darwin 内核版本 16.3.0:11 月 17 日星期四 20:23:58 PST 2016; root:xnu-3789.31.2~1/RELEASE_X86_64
virtualenv scrapy Python 2.7.10
更新
我用新的 virtualenv 做了同样的尝试 Python 3.5.2 使用 Python 3.5.2 我终于得到了正确的结果,没有像其他设置中那样的 unicode 问题。
您看到的是字符串的调试表示,因为您只是在解释器中查看变量而不是打印它。在 Python 2.7 中,所有不可打印的非 ASCII 字符都显示有转义码。在 Python 3 中,只有当前终端编码中可显示的字符才显示为转义码。
打印字符串以强制显示字符。
>>> s=u'\u201cThe world\u201d'
>>> s
u'\u201cThe world\u201d'
>>> print s
“The world”
如果您打印的终端使用的编码不支持非 ASCII 字符,您 可能 得到 UnicodeEncodeError
,但是由于 Python 3.5 适合你,你的终端必须支持它们。
请注意,调试显示还显示了代表 Unicode 字符串的 u
并引用了输出。 print
只显示字符串内容。