Self Referencing is Classy
You probably have noticed that within the spider class, we always input the argument self in the start_requests and parse methods (just look in the sample code in this exercise!). This allows us to reference between methods within the class. That is, if we want to refer to the method parse within the start_requests method, we would need to write self.parse rather than just parse; what writing self does is tell the code: "Look in the same class as start_requests for a method called parse to use."
In this exercise you will get a chance to play with this "self referencing".
This exercise is part of the course
Web Scraping in Python
Exercise instructions
- Fill in the required
scrapyobject into the classYourSpiderneeded to create thescrapyspider. - Pass the string argument
"Hello World!"to fill in the blank in thestart_requestsmethod to use theprint_msgmethod.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import scrapy library
import scrapy
# Create the spider class
class YourSpider( ____ ):
name = "your_spider"
# start_requests method
def start_requests( self ):
self.print_msg( ____ )
# parse method
def parse( self, response ):
pass
# print_msg method
def print_msg( self, msg ):
print( "Calling start_requests in YourSpider prints out:", msg )
# Inspect Your Class
inspect_class( YourSpider )