【发布时间】:2021-09-18 11:29:58
【问题描述】:
我是 Python 新手,目前正在学习使用蜘蛛进行网页抓取。按照教程,我坚持使用 Python 进行相对导入。
这是我当前文件夹的结构(scrapy startproject p1提供):
我的 items.py 文件:
# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.html
import scrapy
class Test(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
title = scrapy.Field()
price = scrapy.Field()
pass
在我的 filetwo.py 中,它包含:
import scrapy
from p1.items import Test
当我运行代码时,我得到“ModuleNotFoundError: No module named 'p1'”
我也在网上看到一些人遇到同样的问题,所以我尝试了..items import Test,但仍然没有工作。它给了我错误:ImportError: attempted relative import with no known parent package
谁能给我一盏灯?
提前致谢!
【问题讨论】:
标签: python scrapy relative-import