Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different parse function for different start_urls in Scrapy

Tags:

scrapy

Can Scrapy set different parse function for every start_urls?

This is the piece of pseudo-code:

    start_urls = [
    "http://111sssssssss.com",
    "http://222sssssssssssss.com",
    "http://333sssssssssss.com",
    "http://444sssssssss.com",
]


def parse_1():
    '''some code, this function will crawl http://111sssssssss.com'''


def parse_2():
    '''some code, this function will crawl http://222sssssssssssss.com'''

Is there any way to do that?

like image 905
jianbing Ma Avatar asked Oct 14 '25 19:10

jianbing Ma


1 Answers

You can override / implement the parse_start_url function and there call parse_1 or parse_2 when the response.url meets your criteria (in this case it is the right URL).

def parse_start_url(response):
    if response.url == 'http://111sssssssss.com':
        parse_1(response)
    if response.url == 'http://222sssssssssssss.com':
        parse_2(response)

For more information about parse_start_url() read the documentation.

like image 112
GHajba Avatar answered Oct 18 '25 06:10

GHajba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!