scrapy 编码问题? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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

scrapy 编码问题?

  •  
  •   ljcarsenal Mar 8, 2015 3838 views
    This topic created in 4070 days ago, the information mentioned may be changed or developed.
    我在爬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 replies    2015-05-14 12:00:08 +08:00
    KyL
        1
    KyL  
       Mar 8, 2015
    虽然症状不像,但是希望你能从 http://v2ex.com/t/163786 得到有用的信息
    Battle
        2
    Battle  
       Mar 8, 2015
    我当初也遇过这个编码问题,我也不懂原因在哪里,不过问题解决了,因为看了一篇博客,然后照着博主的方法敲了一遍,希望对你有帮助,也希望有知道原因的人告诉我一下,附上博客链接:
    http://blog.csdn.net/u012150179/article/details/32911511
    binux
        3
    binux  
       Mar 8, 2015
    上代码,我觉得不是你说的那个问题。
    JoeShu
        4
    JoeShu  
       Mar 8, 2015
    mysql默认编码不是utf8,是Latin1。
    ljcarsenal
        5
    ljcarsenal  
    OP
       Mar 9, 2015
    @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/tt239753/reviews?start=0
    得到的错误信息为 Warning: Incorrect string value: '\x97every...' for column 'content' at row 1,搜这个网页上的every,结果 有7个,应该第三个人评论的那篇文章,前面有个特殊的破折号 every。
    在数据库里无法保存。

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

    没有看到『存到mysql数据库里面』啊
    ljcarsenal
        8
    ljcarsenal  
    OP
       Mar 9, 2015
    @binux 那个gist文件的 65-77行,用的 Django 的orm ,存在时更新,不存在时创建
    yepinf
        9
    yepinf  
       May 14, 2015
    iso-8859-1这个编码,使用urllib2来获取网页,可以正确解码
    昨晚刚解决,不知道为什么:D
    About     Help     Advertise     Blog     API     FAQ     Solana     3215 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 38ms UTC 12:41 PVG 20:41 LAX 05:41 JFK 08:41
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86