V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
chunjie
V2EX  ›  问与答

请问 scrapy 部署到 Ubuntu 服务器上为何会出现 UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 的错误?

  •  
  •   chunjie · Aug 5, 2018 · 2737 views
    This topic created in 2824 days ago, the information mentioned may be changed or developed.
    我把自己的一个 scrapy 项目部署到 Ubuntu16.04 服务器上,我自己的开发环境也是 Ubuntu16.04 ,在自己的开发环境上是没事的,我直接执行 scrapy crawl xxx 是可以的,但是我写了一个 py 文件来循环执行 scrapy crawl xxx 这个命令,我通过这个 py 文件来执行 scrapy 就出现 UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)的错误了,而且我也加了 sys.stdout = codecs.getwriter('utf-8')(sys.stdout.detach())这句还是不行,以下是我的这个 py 文件:

    # -*- coding: utf-8 -*-
    import os
    import multiprocessing
    import time
    import sys
    import codecs

    sys.stdout = codecs.getwriter('utf-8')(sys.stdout.detach())
    print('中文')


    def start_spider_collect():
    # os.system('scrapy crawl xxx')
    # 获取 scrapy 的 spider 列表(获取命令行执行后的输出结果)
    while True:
    result = os.popen('scrapy list')
    res = result.read()
    for line in res.splitlines():
    if str(line).startswith('spider_collect'):
    print(line)
    print('sleep 10s')
    time.sleep(10)
    os.system('scrapy crawl ' + line)


    if __name__ == '__main__':
    multiprocessing.Process(target=start_spider_collect, name='process: start_spider_collect').start()

    请问这是怎么回事?
    6 replies    2018-08-05 17:16:52 +08:00
    chunjie
        1
    chunjie  
    OP
       Aug 5, 2018
    我改成以下后可以了:

    # -*- coding: utf-8 -*-
    import os
    import multiprocessing
    import time
    import sys
    import codecs

    sys.stdout = codecs.getwriter('utf-8')(sys.stdout.detach())
    print('中文')


    def start_spider_collect():
    while True:
    print('sleep 10s')
    time.sleep(10)
    os.system('scrapy crawl spider_collect_fengsheji')


    if __name__ == '__main__':
    multiprocessing.Process(target=start_spider_collect, name='process: start_spider_collect').start()

    但是妈呀,我发现执行这个文件 scrapy 启动后,好像杀掉进程也无法停下了。。。杀掉后过一阵子又会自动重启。。怎么回事?
    thinker3
        2
    thinker3  
       Aug 5, 2018
    python3
    yangyaofei
        3
    yangyaofei  
       Aug 5, 2018 via Android
    用 htop 看看进程树吧……你用你是另启动进程,在进程中执行启动,只杀子进程
    d5
        4
    d5  
       Aug 5, 2018
    pip3 install -r requirements.txt
    python3 xxx.py
    wsc449
        5
    wsc449  
       Aug 5, 2018
    再次设置启动编码识别

    reload(sys)
    sys.setdefaultencoding('utf-8')
    chunjie
        6
    chunjie  
    OP
       Aug 5, 2018
    @wsc449 这是 Python2 的方法吧,Python3 我执行 sys.setdefaultencoding('utf-8')这句都提示 sys 没有 setdefaultencoding 这个方法的,而且我执行 sys.getdefaultencoding()是输出 utf-8 的。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1784 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 16:18 · PVG 00:18 · LAX 09:18 · JFK 12:18
    ♥ Do have faith in what you're doing.