V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
ljcarsenal
V2EX  ›  Python

scrapy 编码问题?

  •  
  •   ljcarsenal · 2015-03-08 16:14:06 +08:00 · 3322 次点击
    这是一个创建于 3351 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我在爬imdb网站,然后存到mysql数据库里面。如这个 http://www.imdb.com/title/tt2474024/reviews?start=0,其中有一段是
    Surprisingly, it's not the talented leads that provide the most interest here … it's the story structure. As per the title, the story follows the couple's relationship over a five year period. The opening scene features Cathy reading and reacting to the break-up note left by Jamie. The second scene features Jamie describing his joy when he first falls for Cathy, as they romp in bed. See, Cathy's story goes from the end to the beginning, while Jamie's story goes from the beginning to the end … intersecting only at the marriage proposal in the park. It's a fascinating way to tell a story – not just two perspectives, but also in reverse order of each other!
    提示存这一段的时候 Warning: Incorrect String value...,是那个省略号的问题。用chrome查看网页编码是 西欧语言windows-1252,用requests请求,可以看到r.encoding=iso-8859-1。我的mysql存储编码是默认的(应该是utf-8?),在代码里 我 是这样写的:
    `content = p.text_content().strip('\n').decode('iso-8859-1').encode('utf8')`
    decode那里换成windows-1252也不对。
    也试过
    `content = unicode(p.text_content().strip('\n'))`,都不行。需要怎么解决?
    9 条回复    2015-05-14 12:00:08 +08:00
    KyL
        1
    KyL  
       2015-03-08 19:13:58 +08:00
    虽然症状不像,但是希望你能从 http://v2ex.com/t/163786 得到有用的信息
    Battle
        2
    Battle  
       2015-03-08 20:00:34 +08:00
    我当初也遇过这个编码问题,我也不懂原因在哪里,不过问题解决了,因为看了一篇博客,然后照着博主的方法敲了一遍,希望对你有帮助,也希望有知道原因的人告诉我一下,附上博客链接:
    http://blog.csdn.net/u012150179/article/details/32911511
    binux
        3
    binux  
       2015-03-08 20:07:52 +08:00
    上代码,我觉得不是你说的那个问题。
    JoeShu
        4
    JoeShu  
       2015-03-08 20:42:10 +08:00
    mysql默认编码不是utf8,是Latin1。
    ljcarsenal
        5
    ljcarsenal  
    OP
       2015-03-09 00:12:01 +08:00
    @binux @JoeShu
    我是用django模型建立的数据表结构(因为后续还要有把数据全部存入excel里的奇葩需求,这里基本上用的都是字符串,包括日期),看了下 默认生成的的确为Latin1编码
    <script src="https://gist.github.com/lijunchengbeyond/ab410191ffa5e2164c31.js"></script>
    运行爬虫
    用下面这句替代 content = unicode(p.text_content().strip('\n')).encode('latin1'),
    请求的是 http://www.imdb.com/title/tt2397535/reviews?start=0
    得到的错误信息为 Warning: Incorrect string value: '\x97every...' for column 'content' at row 1,搜这个网页上的every,结果 有7个,应该第三个人评论的那篇文章,前面有个特殊的破折号 —every。
    在数据库里无法保存。

    爬取其他页面类似,会有类似 é 等这种符号无法保存。
    问题是有没有办法解决,使其能保存在数据库里?
    mindcat
        6
    mindcat  
       2015-03-09 12:22:20 +08:00
    这里有一只猫猫,猫猫什么都不知道 /w\
    猫猫建议你试试`r.encoding = "utf-8"`喵~
    binux
        7
    binux  
       2015-03-09 14:14:49 +08:00
    `content = p.text_content().decode("Windows-1252")`
    在内存中,只使用 unicode 。只在数据边界上进行遍解码。

    没有看到『存到mysql数据库里面』啊
    ljcarsenal
        8
    ljcarsenal  
    OP
       2015-03-09 16:18:58 +08:00
    @binux 那个gist文件的 65-77行,用的 Django 的orm ,存在时更新,不存在时创建
    yepinf
        9
    yepinf  
       2015-05-14 12:00:08 +08:00
    iso-8859-1这个编码,使用urllib2来获取网页,可以正确解码
    昨晚刚解决,不知道为什么:D
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1285 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 58ms · UTC 18:01 · PVG 02:01 · LAX 11:01 · JFK 14:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.