今天在学习scrapy的时候,在网上找了一段代码,运行出了一点问题。

命令行报错:

name 'reload' is not defined

原因是,python版本的问题

原代码如下:

import time
import sys
reload(sys)
sys.setdefaultencoding('utf8') class Meiju100Pipeline(object):
def process_item(self, item, spider):
today = time.strftime('%Y%m%d',time.localtime())
fileName = today + 'movie.txt'
with open(fileName,'a') as fp:
fp.write(item['storyName'][0].encode("utf8") + '\t' + item['storyState'][0].encode("utf8") + '\t' + item['tvStation'][0] + '\t' + item['updateTime'][0] + '\n')
return item

此代码为python2.7版本,

reload(sys)              #重新加载sys模块
sys.setdefaultencoding('utf8')   #设置默认编码格式为utf-8

在3.x版本中,应改成如下:

import time
import sys
import importlib
importlib.reload(sys)
#sys.setdefaultencoding('utf8') class Meiju100Pipeline(object):
def process_item(self, item, spider):
today = time.strftime('%Y%m%d',time.localtime())
fileName = today + 'movie.txt'
with open(fileName,'a') as fp:
fp.write(item['storyName'][0].encode("utf8") + '\t' + item['storyState'][0].encode("utf8") + '\t' + item['tvStation'][0] + '\t' + item['updateTime'][0] + '\n')
return item
设置编码格式的代码可以注释掉,因为3.x版本中默认就是utf-8编码。

name 'reload' is not defined解决方法的更多相关文章

  1. python __file__ is not defined 解决方法

    python __file__ is not defined 解决方法 __file__ 是在python module 被导入的时候生成的一个变量,所以在 __file__ 不能被使用,但是又想获取 ...

  2. Electron "jQuery/$ is not defined" 解决方法

    参考问题:https://stackoverflow.com/questions/32621988/electron-jquery-is-not-defined <!-- Insert this ...

  3. Target runtime Apache Tomcat 7.1 is not defined 解决方法

    在工作台目录下找到 自己操作的项目的文件夹 /.settings/org.eclipse.wst.common.project.facet.core.xml 打开后,显示 <?xml versi ...

  4. jQuery报错:Uncaught ReferenceError: $ is not defined解决方法

    原因及解决办法 1:加载jquery失败,就再次确认jquery的路径是否正确. 2:如果jquery的路径没有问题,那么就可能是浏览器在解释你的js语句时,还没有加载jquery库.所以,你需要将加 ...

  5. 信息: TLD skipped. URI: http://java.sun.com/jstl/* is already defined解决方法

    整合Spring MVC由于用到jstl,所以假如jstl便签用的jar包,启动tomcat时控制台出现了如下的输出: standard.jar与jstl.jar一起使用,但是jstl 1.2版本的就 ...

  6. Couldn't import dot_parser, loading of dot files will not be possible. 解决方法

    参考: pydot and graphviz error: Couldn't import dot_parser, loading of dot files will not be possible ...

  7. 在IE浏览器中执行OpenFlashChart的reload方法时无法刷新的解决方法

    由于项目需求,需要在网页上利用图表展示相关数据的统计信息,采用了OpenFlashChart技术.OpenFlashChart是一款开源的以Flash和Javascript为技术基础的免费图表,用它能 ...

  8. 运行supervisorctl reload报错解决方法

    在进行守护进程时运行supervisorctl reload出现“error: <class 'socket.error'>, [Errno 2] No such file or dire ...

  9. 【java】关于Cannot refer to the non-final local variable list defined in an enclosing scope解决方法

    今天学习中遇到了一个问题: Cannot refer to the non-final local variable list defined in an enclosing scope 这里的new ...

随机推荐

  1. Centos7搭建虚拟用户FTP

    yum install -y vsftpd #安装ftp服务 useradd -s /sbin/nologin virftp #创建用户,用于ftp服务 vim /etc/vsftpd/vsftpd_ ...

  2. 又双叒叕换,微软这次换Edge了

    http://tech.sina.com.cn/it/2018-12-06/doc-ihmutuec6481129.shtml 其实两个月前跟一个微软的前同事聊天已经听说过微软要基于Chromiun来 ...

  3. SpringBoot 项目在静态工具类中注入 RedisTemplate

    静态属性不能直接注入,可以通过其set方法进行注入.(注意生成的set方法需要去掉static). 在工具类里直接注入RedisTemplate,两种方法: (1)使用@Autowired priva ...

  4. Android开发:APK的反编译(获取代码和资源文件)

    一.反编译工具: 1.APKTool: APKTool是由GOOGLE提供的APK编译工具,能够完成反编译及回编译apk的工作.同时,它也有着安装反编译系统apk所需要的framework-res框架 ...

  5. 【Android Studio安装部署系列】十五、Android studio添加Assets目录

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 概述 Android Studio新建项目时是没有assets目录,需要自己手动创建. app右键——New——Folder——Asset ...

  6. SnackbarUtilDemo【Snackbar的封装类】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 这个工具类参考的是<没时间解释了,快使用Snackbar!——Android Snackbar花式使用指南>,代码几乎一 ...

  7. TypeScript 中的方法重载

    方法重载(overload)在传统的静态类型语言中是很常见的.JavaScript 作为动态语言, 是没有重载这一说的.一是它的参数没有类型的区分,二是对参数个数也没有检查.虽然语言层面无法自动进行重 ...

  8. Python的魔法函数系列 __getattrbute__和__getattr__

      #!/usr/bin/env python # -*- coding: utf-8 -*- import sys __metaclass__ = type """ _ ...

  9. MIPI DSI之DBI DPI含义和区别(3-1)

    一.MIPI MIPI(Mobile Industry Processor Interface/移动工业处理器接口)是2003年由ARM.Nokia.ST 等公司成立联盟并为移动应用处理器制定的一个开 ...

  10. 通过 UI 管理 docker

    Docker 正在被用在越来越多的场景中,对于不太习惯命令行工具的朋友来说,docker cli 用起来可能会比较吃力.本文笔者将介绍一个功能强大的 docker web 客户端:portainer( ...