Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果嵌套了多个代码,有什么方法可以提升代码?_Python - Fatal编程技术网

Python 如果嵌套了多个代码,有什么方法可以提升代码?

Python 如果嵌套了多个代码,有什么方法可以提升代码?,python,Python,我用嵌套的if-else案例编写了这段代码,但我觉得它太难看了,不知道是否有任何方法可以改进它(或者有更好的方法来实现它) 主要的问题是我想从响应中提取一个url/链接,然后拆分它并获取第一个元素。但是url只存在于两个(或更多)元素中的一个元素中。我不能直接进行拆分,因为url可能是非类型的。我想尝试一下尝试一下,除了别的,但这似乎变得更复杂了 有更好的解决方案吗?简化此代码的最佳方法是首先选择两个类: def do_something(self, response): a_url

我用嵌套的
if-else
案例编写了这段代码,但我觉得它太难看了,不知道是否有任何方法可以改进它(或者有更好的方法来实现它)

主要的问题是我想从响应中提取一个url/链接,然后拆分它并获取第一个元素。但是url只存在于两个(或更多)元素中的一个元素中。我不能直接进行拆分,因为url可能是
非类型的
。我想尝试一下
尝试一下,除了别的
,但这似乎变得更复杂了


有更好的解决方案吗?

简化此代码的最佳方法是首先选择两个类:

def do_something(self, response):  
    a_url = response.css("a.classA::attr(href), a.classB::attr(href)")
    if a_url:   
        yield Request(
            url=a_url.split('&')[0],
            dont_filter=True,
            callback=self.do_next_thing
        )
    else:
        logger.error('get no url')

简化此代码的最佳方法是首先在scrasty中选择两个类:

def do_something(self, response):  
    a_url = response.css("a.classA::attr(href), a.classB::attr(href)")
    if a_url:   
        yield Request(
            url=a_url.split('&')[0],
            dont_filter=True,
            callback=self.do_next_thing
        )
    else:
        logger.error('get no url')

我想你可以这样做:

def do_something(self, response):
    a_url = (
        response.css('a.classA::attr(href)').extract_first()
        or
        response.css('a.classB::attr(href)').extract_first()
    )

    if not a_url:
        logger.error('get no url')
        return # or raise an exception and let the caller do the logging

    yield Request(
        url=a_url.split('&')[0],
        dont_filter=True,
        callback=self.do_next_thing
    )
def prepare_something(self, response):
  a_url = response.css('a.classA::attr(href)').extract_first()
  if a_url:
    return a_url.split('&')[0]
  else:
    a_url = response.css('a.classB::attr(href)').extract_first()
    if a_url:
      return a_url.split('&')[0]
    else:
      logger.error('get no url')
      return None


def do_something(self, response):
  a_url = self.prepare_something(response)
  if a_url:
    yield Request(
      url=a_url,
      dont_filter=True,
      callback=self.do_next_thing
    )
这使用了以下部件的短路行为:

表达式
x
y
首先计算
x
;如果
x
为真,则其 返回值;否则,将计算
y
,并计算结果值 他回来了


它还使用了“提前返回”技术,即先处理故障案例,然后在任何
if
else
之外处理“正常”案例。我认为您可以这样做:

def do_something(self, response):
    a_url = (
        response.css('a.classA::attr(href)').extract_first()
        or
        response.css('a.classB::attr(href)').extract_first()
    )

    if not a_url:
        logger.error('get no url')
        return # or raise an exception and let the caller do the logging

    yield Request(
        url=a_url.split('&')[0],
        dont_filter=True,
        callback=self.do_next_thing
    )
def prepare_something(self, response):
  a_url = response.css('a.classA::attr(href)').extract_first()
  if a_url:
    return a_url.split('&')[0]
  else:
    a_url = response.css('a.classB::attr(href)').extract_first()
    if a_url:
      return a_url.split('&')[0]
    else:
      logger.error('get no url')
      return None


def do_something(self, response):
  a_url = self.prepare_something(response)
  if a_url:
    yield Request(
      url=a_url,
      dont_filter=True,
      callback=self.do_next_thing
    )
这使用了以下部件的短路行为:

表达式
x
y
首先计算
x
;如果
x
为真,则其 返回值;否则,将计算
y
,并计算结果值 他回来了


它还使用了“早期返回”技术,即先处理失败情况,然后在“<代码> > < <代码> >或<代码>其他> /代码>的情况下完成“正常”的情况。

您可能需要考虑将方法分割为两个(稍后甚至三个)。因为在我看来,第一行更多的是准备,而不是实际的逻辑。大概是这样的:

def do_something(self, response):
    a_url = (
        response.css('a.classA::attr(href)').extract_first()
        or
        response.css('a.classB::attr(href)').extract_first()
    )

    if not a_url:
        logger.error('get no url')
        return # or raise an exception and let the caller do the logging

    yield Request(
        url=a_url.split('&')[0],
        dont_filter=True,
        callback=self.do_next_thing
    )
def prepare_something(self, response):
  a_url = response.css('a.classA::attr(href)').extract_first()
  if a_url:
    return a_url.split('&')[0]
  else:
    a_url = response.css('a.classB::attr(href)').extract_first()
    if a_url:
      return a_url.split('&')[0]
    else:
      logger.error('get no url')
      return None


def do_something(self, response):
  a_url = self.prepare_something(response)
  if a_url:
    yield Request(
      url=a_url,
      dont_filter=True,
      callback=self.do_next_thing
    )
这样一来,imho,代码就更干净了,您可以看到您可能想要重构
prepare\u something
方法,如下所示:

def get_a_url_part(self, response, path):
  a_url = response.css(path).extract_first()
  return a_url.split('&')[0] if a_url else None

def prepare_something(self, response):
  a_url = self.get_a_url_part(response, 'a.classA::attr(href)')
  b_url = self.get_a_url_part(response, 'a.classB::attr(href)')
  return a_url if a_url else b_url

def do_something(self, response):
  a_url = self.prepare_something(response)
  if a_url:
    yield Request(
      url=a_url,
      dont_filter=True,
      callback=self.do_next_thing
   )
在我看来,这可以被认为是一种进步


< p>你可能想考虑把方法分成两个(以后甚至三个)。因为在我看来,第一行更多的是准备,而不是实际的逻辑。大概是这样的:

def do_something(self, response):
    a_url = (
        response.css('a.classA::attr(href)').extract_first()
        or
        response.css('a.classB::attr(href)').extract_first()
    )

    if not a_url:
        logger.error('get no url')
        return # or raise an exception and let the caller do the logging

    yield Request(
        url=a_url.split('&')[0],
        dont_filter=True,
        callback=self.do_next_thing
    )
def prepare_something(self, response):
  a_url = response.css('a.classA::attr(href)').extract_first()
  if a_url:
    return a_url.split('&')[0]
  else:
    a_url = response.css('a.classB::attr(href)').extract_first()
    if a_url:
      return a_url.split('&')[0]
    else:
      logger.error('get no url')
      return None


def do_something(self, response):
  a_url = self.prepare_something(response)
  if a_url:
    yield Request(
      url=a_url,
      dont_filter=True,
      callback=self.do_next_thing
    )
这样一来,imho,代码就更干净了,您可以看到您可能想要重构
prepare\u something
方法,如下所示:

def get_a_url_part(self, response, path):
  a_url = response.css(path).extract_first()
  return a_url.split('&')[0] if a_url else None

def prepare_something(self, response):
  a_url = self.get_a_url_part(response, 'a.classA::attr(href)')
  b_url = self.get_a_url_part(response, 'a.classB::attr(href)')
  return a_url if a_url else b_url

def do_something(self, response):
  a_url = self.prepare_something(response)
  if a_url:
    yield Request(
      url=a_url,
      dont_filter=True,
      callback=self.do_next_thing
   )
在我看来,这可以被认为是一种进步

问候:)