Jshee
Jshee

Reputation: 2686

import my scrapy script throwing cannot import on method

Im at a mental block of how I can import my python class currently saved as Scrapese.py.

import scrapy

class Scrapese(scrapy.Spider):
    name = 'scrape-se'
    seach_engine = [
        'se1.com',
        'se2.com',
    ]

    def parse(self, seach_engine, site_to_parse, page_start, page_end, response):
        site = str(seach_engine+site_to_parse)
        if site_to_parse == seach_engine[0]:
            print("executing against se1!")
        elif site_to_parse == searh_engine[1]:
            print("executing against se2!")
        else:
            print("Something bad happened.")

I keep trying to typical:

from Scrapese import parse

but it says:

ImportError: cannot import name 'parse'

What am I doing wrong?

Thanks

Upvotes: 0

Views: 78

Answers (1)

Tagc
Tagc

Reputation: 9072

Scrapese is the name of a Python module in which you define a class also called Scrapese.

The line from Scrapese import parse will cause the Python interpreter to try importing a module called Scrapese and looking in that for an object parse.

What you probably want to do is the following:

# Scrapese.py

class Scrapese(object):
    def parse(self):
        pass

# main.py

from Scrapese import Scrapese

o = Scrapese()
o.parse()

This will cause the Python interpreter to make the Scrapese class definition available within another script (main.py), which you can then instantiate and use for parsing.

Upvotes: 1

Related Questions