【问题标题】:Scrapy "Quotes Tutorial" - Unicode in extracted textScrapy “报价教程” - 提取文本中的 Unicode
【发布时间】:2017-05-03 02:53:52
【问题描述】:

我按照 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."'
>>> 

我不确定我在这里做错了什么,只是按照文档进行操作。有什么要在配置文件中设置的,或者我该如何解决这个问题? 没有提到解码/编码 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))

输出sn-p:

{'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:2016 年 11 月 17 日星期四 20:23:58 PST;根:xnu-3789.31.2~1/RELEASE_X86_64

虚拟环境爬虫 Python 2.7.10

更新

我对新的 virtualenv Python 3.5.2 进行了同样的尝试 使用 Python 3.5.2,我终于得到了正确的结果,没有像其他设置那样的 unicode 问题。

【问题讨论】:

    标签: python-2.7 unicode scrapy scrapy-spider


    【解决方案1】:

    您看到的是字符串的调试表示,因为您只是在解释器中查看变量而不是打印它。在 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 只显示字符串内容。

    【讨论】:

    • 感谢您的回答;)
    猜你喜欢
    • 2012-09-22
    • 1970-01-01
    • 1970-01-01
    • 2017-07-16
    • 1970-01-01
    • 2015-03-01
    • 2013-08-25
    • 2010-09-17
    • 1970-01-01
    相关资源
    最近更新 更多