Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

when I set start_urls inside a Scrapy spider class, the fllowing code is OK:

class InfoSpider(scrapy.Spider):
    name = 'info'
    allowed_domains = ['isbn.szmesoft.com']
    isbns = list(set(pd.read_csv('E:/books.csv')['ISBN']))
    url = 'http://isbn.szmesoft.com/isbn/query?isbn='
    start_urls = [url + isbns[0]]

But then I got the error Scrapy: NameError: name 'url' is not defined when I rewrite my code as follows:

class InfoSpider(scrapy.Spider):
    name = 'info'
    allowed_domains = ['isbn.szmesoft.com']
    isbns = list(set(pd.read_csv('E:/books.csv')['ISBN']))
    url = 'http://isbn.szmesoft.com/isbn/query?isbn='
    start_urls = [url + isbn for isbn in isbns[:3]]

Maybe I can solve this problem in other ways,but I want to know the reason for the ERROR

There are only four ranges in Python: LEGB, because the local scope of the class definition and the local extent of the list derivation are not nested functions, so they do not form the Enclosing scope.

Therefore, they are two separate local scopes that cannot be accessed from each other.

You need to pass string of it and try printing url so that you can also go and check it on browser if ut actually exists or not.

start_urls = [url + str(isbn) for isbn in isbns[:3]]
print(start_urls)
        self.name = 'info'
        self.allowed_domains = ['isbn.szmesoft.com']
        self.isbns = list(set(pd.read_csv('E:/books.csv')['ISBN']))
        self.url = 'http://isbn.szmesoft.com/isbn/query?isbn='
        self.start_urls = [url + isbn for isbn in isbns[:3]]

Then when you call it do self. before it

the first code 'url' is defined but the second is not, the only difference is the last line – MJ_0826 Aug 10, 2018 at 4:35

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.