【发布时间】: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'}
我从以下网站抓取:
文档 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