0

How do I actually pass data into parse for my spider, le's say variable name or temp.

class CSpider(scrapy.Spider):

    name = "s1"
    allowed_domains = ["abc.com"]
    temp = ""
    start_urls = [
        url.strip() for url in lists
    ]
    def parse(self, response):
        //How do i pass data into here, eg name, temp
Tomer Shetah
  • 7,646
  • 6
  • 20
  • 32
CodeGuru
  • 3,286
  • 10
  • 49
  • 93

2 Answers2

1

If you are defining the temp variable as a class-level variable, you can access it via self.temp.

If this is something you want to be passed from a command-line, see the following topics:

Community
  • 1
  • 1
alecxe
  • 414,977
  • 106
  • 935
  • 1,083
0

As alecxe answered, you can use attributes (class-level variables) to make variables or constants accessible wherever in your class or you can also add a parameter to your method (functions of a class) parse if you want to be able to give values to that parameter that would come from outside of the class.

I'll try here to give you an example of your code with both solutions.

Using an attribute:

class CSpider(scrapy.Spider):

    name = "s1"
    allowed_domains = ["abc.com"]
    temp = ""

    # Here is our attribute
    self.number_of_days_in_a_week = 7

    start_urls = [
        url.strip() for url in lists
    ]
    def parse(self, response):
        # It is now used in the method
        print(f"In a week, there is {self.number_of_days_in_a_week} days.")

If you need to, here is how to pass it as an other argument:

class CSpider(scrapy.Spider):

    name = "s1"
    allowed_domains = ["abc.com"]
    temp = ""
    start_urls = [
        url.strip() for url in lists
    ]
    def parse(self, what_you_want_to_pass_in):
        print(f"In a week, there is {what_you_want_to_pass_in} days.")

# We create an instance of the spider
spider1 = CSpider

# Then we use it's method with an argument
spider1.parse(7)

Note that in the second example, I took back the response argument from your parse method because it was easier to show how the arguments would be passed. Still, if you consider the entire Scrapy framework, you can for sure add external values using this solution.

LukeRain
  • 15
  • 5