0
votes

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!

1

1 Answers

0
votes
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.