【问题标题】:python : ImportError: No module named utils with clean_stringpython:ImportError:没有名为utils的模块带有clean_string
【发布时间】:2016-09-04 06:22:23
【问题描述】:

注意:我已经检查了与我的错误相同的问题,但我的不同我想问是否“clean_string,clean_number,clean_text,clean_float,clean_int”

agency_id = scrapy.Field(serializer=clean_string)

是python中的一些内置函数,或者我必须导入才能使其工作

我是 python 新手,只是做一些编程工作。

下面是我的代码sn-p

from .utils import clean_string, clean_number, clean_text, clean_float, clean_int
from six.moves.urllib.parse import urljoin

class MyItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    agency_id = scrapy.Field(serializer=clean_string)

当我运行上面的代码时,它给了我错误

**ImportError: No module named utils**

你能帮我安装 clean_string 什么的吗

【问题讨论】:

标签: python


【解决方案1】:

根据我们的讨论,请安装python -m pip install pyes

按如下方式进行:

from pyes import utils

# use it like below
class MyItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    agency_id = scrapy.Field(serializer=utils.clean_string)

(或)

from pyes.utils import clean_string

# use it like below
class MyItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    agency_id = scrapy.Field(serializer=clean_string)

您不能使用from .utils import clean_string,因为它会在当前工作目录中查找utils。而是使用from django import utils(或)使用from pyes.utils import clean_string

【讨论】:

  • 让我安装 django
  • 如果您正在开发或编写此代码作为 django Web 应用程序的一部分。是的,你必须通过python -m pip install django 安装django。
  • 我确实使用你的命令安装并运行代码现在它给出了错误:from django.utils import clean_string, clean_number, clean_text, clean_float, clean_int ImportError: cannot import name clean_string
  • 您是否正在开发这个以使用 scrapy 从网页中提取数据?因为我看到它是scrapy.Item?请确认
  • 是的,我正在使用 scrapy.org
最近更新 更多