简体   繁体   中英

ValueError: attempted relative import beyond top-level package (Scrapy)

I've been trying to write a Python file to scrape the whole content of a page of a website. Now, everything seems to be fine in my code, until I run it.

I've made sure to link the items from the items python file. I shouldn't get any errors, but yet I keep getting "ValueError: attempted relative import beyond top-level package"

Here is my code from my main python file:

import scrapy
from ..items import AnalogicScrapeItem


class AnalogicSpider(scrapy.Spider):
    name = 'analogic'
    start_urls = ['https://www.analogic.com/about/']

    def parse(self, response):
        items = AnalogicScrapeItem()
        body1 = response.css('body').css('::text').extract()

        items['body1'] = body1

        yield items

Here is my code from items.py file:

import scrapy


class AnalogicScrapeItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    body1 = scrapy.Field()

After running the code, here is the error I get:

Traceback (most recent call last):
  File "C:/Users/Kev/PycharmProjects/whole_page_extract3/analogic_scrape/
        analogic_scrape/spiders/analogic.py", line 3, in <module> 
        from ..items import AnalogicScrapeItem
        ValueError: attempted relative import beyond top-level package

Any help resolving this issue would be greatly appreciated, thank you!

from analogic_scrape.items import AnalogicScrapeItem

would do the job. When you use .. , you are importing files from a relative path.

However, if you run the script from command line with scrapy crawl analogic , relative imports are not a problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM