Different parse function for different start_urls in Scrapy

房东的猫 提交于 2020-01-13 18:24:09

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/32246864/different-parse-function-for-different-start-urls-in-scrapy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!